JDBC工具类——常规操作封装(可以直接复制代码使用)
- 制作工具类
- 封装操作方法,方便操作调用,避免重复操作
- 了解配置文件
工具类
主方法
package JDBCUtils;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String url = null;
private static String user = null;
private static String password = null;
private static String driver = null;
static {
try {
Properties properties = new Properties();
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbcproperties.properties");
String path = res.getPath();
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 (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
配置文件
url=jdbc:mysql:/
user=root
password=root
driver=com.mysql.jdbc.Driver
使用实例
import JDBCUtils.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JDBCDemo4 {
public static void main(String[] args) {
String sql = null;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getConnection();
statement = connection.createStatement();
sql = "SELECT * FROM STUDENT3";
resultSet = statement.executeQuery(sql);
List<student3Findall> list = null;
student3Findall student3Findalls = null;
list = new ArrayList<student3Findall>();
while (resultSet.next()) {
student3Findalls = new student3Findall();
int id = resultSet.getInt(1);
String name = resultSet.getString(2);
int age = resultSet.getInt(3);
String sex = resultSet.getString(4);
String address = resultSet.getString(5);
int math = resultSet.getInt(6);
int english = resultSet.getInt(7);
student3Findalls.setId(id);
student3Findalls.setAge(age);
student3Findalls.setSex(sex);
student3Findalls.setEnglish(english);
student3Findalls.setMath(math);
student3Findalls.setName(name);
student3Findalls.setAddress(address);
list.add(student3Findalls);
}
System.out.println(list);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.close(resultSet, statement, connection);
}
}
}