You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.1 KiB
91 lines
2.1 KiB
package com.utils;
|
|
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.ResultSet;
|
|
import java.sql.Statement;
|
|
|
|
|
|
public class JDBCUtils {
|
|
|
|
private static String driver = "com.mysql.jdbc.Driver";
|
|
private static String url = "jdbc:mysql://localhost:3306/lsyqfksys";
|
|
private static String user = "root";
|
|
private static String psw = "105293";
|
|
|
|
public static Connection getConnection(){
|
|
Connection connection = null;
|
|
try {
|
|
Class.forName(driver);
|
|
connection = DriverManager.getConnection(url, user, psw);
|
|
System.out.println("连接成功");
|
|
}
|
|
catch (Exception e){
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return connection;
|
|
}
|
|
|
|
//释放资源,对于查询
|
|
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
|
|
if (resultSet != null) {
|
|
try {
|
|
resultSet.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
if (statement != null) {
|
|
try {
|
|
statement.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
if (connection != null) {
|
|
try {
|
|
connection.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
//释放资源,对于更新
|
|
public static void close(Statement statement, Connection connection) {
|
|
if (statement != null) {
|
|
try {
|
|
statement.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
if (connection != null) {
|
|
try {
|
|
connection.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
//关闭资源,用于查询
|
|
public static void close(ResultSet resultSet) {
|
|
if (resultSet != null) {
|
|
try {
|
|
resultSet.close();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|