jdbc如何連接資料庫? (附代碼)
這篇文章帶給大家的內容是jdbc如何連接資料庫? (附代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
JDBC簡介
JDBC全稱為:Java Data Base Connectivity (java資料庫連接),可以為多種資料庫提供填統一的訪問。 JDBC是sun開發的一套資料庫存取程式設計接口,是一種SQL級的API。它是由java語言編寫完成,所以具有很好的跨平台特性,使用JDBC編寫的資料庫應用程式可以在任何支援java的平台上運行,而不必在不同的平台上編寫不同的應用程式。 【影片教學推薦:Java教學】
JDBC程式設計步驟
(1)載入驅動程式:
下載驅動包: http://dev.mysql.com/downloads/connector/j/
解壓縮,得到jar檔。將該檔案複製到Java工程目錄Java Resources/Libraries/ 下,→ buildpath 。
(2)取得資料庫連線
(3)建立Statement物件:
(4)傳送SQL指令
#111 )處理資料庫的回傳結果(ResultSet類別)
package com.baidu.emp.jdbcTest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.Driver; /** * 开始使用jdbc连接数据库 * @author Admin * */ public class Test001 { public static void main(String[] args) throws Exception { /** * 加载驱动 */ // 方法一: /* * import java.sql.DriverManager; import com.mysql.jdbc.Driver; */ // Driver driver = new Driver(); // DriverManager.registerDriver(driver); // 方法二:(推荐使用) Class.forName("com.mysql.jdbc.Driver"); /** * 创建链接 */ String url = "jdbc:mysql://localhost:3306/testjdbc"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url, user, password); // 创建statement对象 Statement statement = connection.createStatement(); /** * 执行SQL,获取结果集 */ String sql = "select * from test01"; ResultSet result = statement.executeQuery(sql); // 遍历结果集 while (result.next()) { String name = result.getString("name"); int id = result.getInt("id"); System.out.println(name + "\t" + id); } /** * 关闭链接,释放资源 */ result.close(); statement.close(); connection.close(); } }
package com.boya.emp.jdbcTest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * SQL注入,使用prepareStatement对象进行预编译 * @author Admin * */ public class Test002 { public static void main(String[] args) throws Exception { /** * 加载驱动 */ Class.forName("com.mysql.jdbc.Driver"); /** * 创建链接 */ String url = "jdbc:mysql://localhost:3306/testjdbc"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url, user, password); // 写SQL String sql = "select * from test01 where id = ?"; //创建statement对象,预编译 PreparedStatement statement = connection.prepareStatement(sql); //设置参数 statement.setInt(1, 2); /** * 执行SQL,获取结果集 */ ResultSet result = statement.executeQuery(); // 遍历结果集 while (result.next()) { String name = result.getString("name"); int id = result.getInt("id"); System.out.println(name + "\t" + id); } /** * 关闭链接,释放资源 */ result.close(); statement.close(); connection.close(); } }
driverName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/testjdbc userName=root password=root
package com.baidu.emp.utils; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import org.junit.Test; public class JdbcUtils { static String driverClassName; static String url; static String user; static String password; static { // 创建配置文件对象 Properties properties = new Properties(); // 加载配置文件输入流 InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); // 重新加载配置文件 try { properties.load(inputStream); // 获取配置文件的值 driverClassName = properties.getProperty("driverName"); url = properties.getProperty("url"); user = properties.getProperty("userName"); password = properties.getProperty("password"); Class.forName(driverClassName); } catch (Exception e) { // 抛出异常 throw new RuntimeException(e); } } /** * 获取连接 */ @Test public void testName() throws Exception { System.out.println(driverClassName); } public static Connection getConnection() { Connection connection = null; try { connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { // 抛出异常 throw new RuntimeException(e); } return connection; } /** * 关闭链接,释放资源 */ public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) { try { if (resultSet != null) { resultSet.close(); } resultSet = null; // 垃圾及时清除 //注意,不要弄成死循环 close(connection, statement); } catch (SQLException e) { throw new RuntimeException(e); } } /** * 增删改释放资源 */ public static void close(Connection connection, PreparedStatement statement) { try { if (connection != null) { connection.close(); } connection = null; if (statement != null) { statement.close(); } statement = null; } catch (SQLException e) { throw new RuntimeException(e); } } }
package com.baidu.emp.jdbcTest; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.baidu.emp.utils.JdbcUtils; /** * 使用jdbcUtils连接数据库进行增删改查 * * @author Admin * */ public class Test003 { // 初始化值 Connection connection = null; PreparedStatement statement = null; ResultSet result = null; @Before public void start() throws Exception { // 创建链接 connection = JdbcUtils.getConnection(); System.out.println("创建链接"); } @After public void end() throws Exception { // 关闭链接 JdbcUtils.close(connection, statement, result); System.out.println("关闭链接"); } /** *插入数据 * @throws Exception */ @Test public void add() throws Exception { String sql = "insert into test01 values(null,?)"; statement = connection.prepareStatement(sql); statement.setString(1, "李四"); int result = statement.executeUpdate(); if (result!=0) { System.out.println("添加成功"); } } /** * 删除数据 * @throws Exception */ @Test public void del() throws Exception { String sql = "delete from test01 where id =?"; statement = connection.prepareStatement(sql); statement.setInt(1,3); int result = statement.executeUpdate(); if (result!=0) { System.out.println("删除成功"); } } /** * 修改数据 * @throws Exception */ @Test public void change() throws Exception { String sql = "update test01 set name = ? where id = ?"; statement = connection.prepareStatement(sql); statement.setString(1, "张飞"); statement.setInt(2, 2); int result = statement.executeUpdate(); if (result!=0) { System.out.println("修改成功"); } } /** * 查询全部数据 * @throws Exception */ @Test public void findAll() throws Exception { String sql = "select id , name from test01"; statement = connection.prepareStatement(sql); result = statement.executeQuery(); if (result.next()) { System.out.println("查询成功"); } } /** * 条件查询数据 * @throws Exception */ @Test public void findOne() throws Exception { String sql = "select id , name from test01 where id = ?"; statement = connection.prepareStatement(sql); statement.setInt(1, 2); result = statement.executeQuery(); if (result.next()) { System.out.println("查询成功"); } } }
以上是jdbc如何連接資料庫? (附代碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java8-291之後,禁用了TLS1.1,使JDBC無法用SSL連接SqlServer2008怎麼辦,以下是解決辦法修改java.security檔案1.找到jre的java.security檔案如果是jre,在{JAVA_HOME}/jre/ lib/security中,例如????C:\ProgramFiles\Java\jre1.8.0_301\lib\security如果是Eclipse綠色免安裝便攜版在安裝資料夾搜尋java.security,例如?????xxx\plugins \org

一、說明在JDBC中,executeBatch這個方法可以將多個dml語句批次執行,效率比單一執行executeUpdate高很多,這是什麼原理呢?在mysql和oracle中又是如何實現批次執行的呢?本文將為大家介紹背後的原理。二、實驗介紹本實驗將透過以下三步驟進行a.記錄jdbc在mysql中批量執行和單條執行的耗時b.記錄jdbc在oracle中批量執行和單條執行的耗時c.記錄oracleplsql批量執行和單條執行的耗時相關java和資料庫版本如下:Java17,Mysql8,Oracl

一、資料庫程式設計的必備條件程式語言,如Java,C、C++、Python等資料庫,如Oracle,MySQL,SQLServer等資料庫驅動套件:不同的資料庫,對應不同的程式語言提供了不同的資料庫驅動包,如:MySQL提供了Java的驅動包mysql-connector-java,需要基於Java操作MySQL即需要該驅動包。同樣的,要基於Java操作Oracle資料庫則需要Oracle的資料庫驅動包ojdbc。二、Java的資料庫程式設計:JDBCJDBC,即JavaDatabaseConnectiv

隨著Java的廣泛應用,Java程式在連接資料庫時經常會出現JDBC錯誤。 JDBC(JavaDatabaseConnectivity)是Java中用於連接資料庫的程式設計接口,因此,JDBC錯誤是在Java程式與資料庫互動時遇到的錯誤。以下將介紹一些最常見的JDBC錯誤及如何解決和避免它們。 ClassNotFoundException這是最常見的JDBC

近年來,Java語言的應用越來越廣泛,而JDBCAPI是Java應用程式中與資料庫互動的一種創意方法,JDBC基於一種名為ODBC的開放資料庫連接標準,使得Java應用程式能夠連接到任何資料庫管理系統(DBMS)。其中,MySQL更是一款備受青睞的資料庫管理系統。然而,連接MySQL資料庫時,開發人員也會遇到一些常見問題,本文旨在介紹JDBCAPI連接M

如何使用PHP讀取資料庫中的前幾筆記錄?在開發Web應用程式時,我們經常需要從資料庫中讀取資料並展示給使用者。有時候,我們只需要顯示資料庫中的前幾筆記錄,而不是全部。本文將教您如何使用PHP讀取資料庫中的前幾筆記錄,並提供具體的程式碼範例。首先,假設您已經連接到資料庫並選擇了要操作的表。以下為一個簡單的資料庫連接範例:

在Java程式中,連接資料庫是很常見的操作。雖然連接資料庫能夠使用現成的類別庫和工具,但在程式開發時仍然有可能出現各種異常情況,其中SQLException異常就是其中一種情況。 SQLException是Java提供的一個異常類,它描述了在存取資料庫時發生的錯誤,例如查詢語句錯誤、表不存在、連接中斷等。對於Java程式設計師來說,特別是那些使用JDBC(Java數

go語言透過匯入資料庫驅動、建立資料庫連線、執行SQL語句、使用預處理語句和事務處理等步驟來連接資料庫。詳細介紹:1、導入資料庫驅動,使用github.com/go-sql-driver/mysql包來連接MySQL資料庫;2、建立資料庫連接,提供資料庫的連接信息,包括資料庫的地址、使用者名稱、密碼等再透過sql.Open函數來建立資料庫連線等等。
