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();
}
}
}
}