抽取JDBC工具類
目的:方便代碼書寫
分析:
- 註冊驅動
- 抽取一個方法來獲取連接對象
- 抽取一個方法來釋放資源
配置(properties)文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/city
user=root
password=root
類代碼
package jdbcutils;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtil {
private static String url;
private static String user;
private static String password;
private static String driver;
static {
Properties properties = new Properties();
try {
ClassLoader classLoader = JDBCUtil.class.getClassLoader();
URL jdbcUrl = classLoader.getResource("jdbc.properties");
String path = jdbcUrl.getPath();
path = java.net.URLDecoder.decode(path, "utf-8");
properties.load(new FileReader(path));
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
driver = properties.getProperty("driver");
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void close(Statement statement, Connection connection) {
if (statement != null) {
close(statement);
}
if (connection != null) {
close(connection);
}
}
public static void close(Statement statement, Connection connection, ResultSet resultSet) {
if (statement != null) {
close(statement);
}
if (connection != null) {
close(connection);
}
if (resultSet != null) {
close(resultSet);
}
}
public static void close(Statement statement) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet resultSet) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
工具類測試代碼
package jdbc;
import jdbcutils.JDBCUtil;
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) {
Connection connection = JDBCUtil.getConnection();
Statement statement = null;
ResultSet resultSet = null;
try {
statement = connection.createStatement();
String querySQL = "SELECT * FROM student WHERE id = 1";
resultSet = statement.executeQuery(querySQL);
while(resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String sex = resultSet.getString("sex");
String address = resultSet.getString("address");
int math = resultSet.getInt("math");
int english = resultSet.getInt("english");
System.out.println(id + " " + name + " " + age + " " + sex + " " + address + " " + math + " " + english);
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBCUtil.close(statement, connection, resultSet);
}
}
工具類測試(登陸)
import java.sql.*;
import java.util.Scanner;
public class JDBCDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Connection connection = JDBCUtil.getConnection();
Statement statement = null;
ResultSet resultSet = null;
try {
statement = connection.createStatement();
String user, password;
while(scanner.hasNext()) {
user = scanner.next();
password = scanner.next();
String querySQL = "SELECT password FROM users WHERE name = " + "'" + user + "'";
resultSet = statement.executeQuery(querySQL);
if(!resultSet.next()) {
System.out.println("賬號不存在!");
continue;
}
if(password.trim().equals(resultSet.getString("password").trim())) {
System.out.println("登陸成功!");
} else {
System.out.println("登陸失敗!");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
JDBCUtil.close(connection, statement, resultSet);
scanner.close();
}
}