数据库增删查改方法
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
//三剑客
private static Connection con = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
//四大金刚
private static String driver ="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/usystem";
private static String user="root";
private static String password="root";
//获得连接
public static Connection getConnection(){
try {
Class.forName(driver);
con = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("连接数据库失败");
e.printStackTrace();
}
return con;
}
//关闭连接(查询)
public static void close(Connection con,PreparedStatement ps,ResultSet rs){
try {
rs.close();
ps.close();
con.close();
} catch (SQLException e) {
System.out.println("关闭失败");
e.printStackTrace();
}
}
//关闭连接(增删改)
public static void close(Connection con,PreparedStatement ps){
try {
ps.close();
con.close();
} catch (SQLException e) {
System.out.println("关闭失败");
e.printStackTrace();
}
}
}