Java DataBase Connectivity
Java數據庫連接
寫在前面
- 注冊驅動Class.forName()
- 獲取連接DriverManager.getConnection()
詳細過程
- mysql連接的url
String urlString = "jdbc:mysql://localhost:3306/數據庫名";
- 方式一
//創建mysql驅動對象
Driver driver = new com.mysql.jdbc.Driver();
//設置訪問數據庫的用戶名和密碼
Properties properties = new Properties();
properties.setProperty("user", "用戶名");
properties.setProperty("password", "密碼");
//建立連接
Connection connection = driver.connect(urlString, properties);
//關閉連接
connection.close();
- 方式二
//創建mysql驅動對象
Driver driver = new com.mysql.jdbc.Driver();
//注冊驅動
DriverManager.registerDriver(driver);
//通過DriverManager獲取連接對象
Connection connection = DriverManager.getConnection(urlString, "用戶名", "密碼");
//關閉連接
connection.close();
- 方式三
//注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//獲取連接
Connection connection = DriverManager.getConnection(urlString, "用戶名", "密碼");
//關閉連接
connection.close();
自定義的JdbcUtil類用來獲取Connection對象
- properties文件
url=jdbc:mysql://localhost:3306/javawu
user=root
password=root
driver=com.mysql.jdbc.Driver
- JdbcUtil類
package com.javawu.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcUtil {
private static String driverString;
private static String urlString;
private static String userString;
private static String passwordString;
static {
try {
FileInputStream fileInputStream = new FileInputStream("./bin/db.properties");
Properties properties = new Properties();
properties.load(fileInputStream);
urlString = properties.getProperty("url");
driverString = properties.getProperty("driver");
userString = properties.getProperty("user");
passwordString = properties.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//獲取連接對象
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName(driverString);
connection = DriverManager.getConnection(urlString, userString, passwordString);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
//關閉相關對象、
public static void close(Connection connection, Statement statement) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}