package common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private Connection conn;
private Statement stmt;
private ResultSet result;
public DBUtil() {
String className="com.mysql.jdbc.Driver";//��������
try{
Class.forName(className);
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.print("h");
}
}
/**
*
* @return
*/
public Connection getConn(){
String url = "jdbc:mysql://localhost:3306/tb?characterEncoding=utf-8";
String user = "root";
String password = "root";
try{
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.print("执行数据库失败");
}
return conn;
}
public Statement getStmt(){
try {
if(conn == null){
conn = this.getConn();//���û���������ݿ⣬����������
}
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
System.out.print("����Statement����ʧ��");
}
return stmt;
}
public int executeUpdate(String sql){
int row = -1;
try {
if(stmt == null){
stmt = this.getStmt();//���ô���Statement����ķ��������ֳ�����
}
row = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return row;
}
public ResultSet executeQuery(String sql){
try {
if(stmt == null){
stmt = this.getStmt();//���ô���Statement����ķ��������ֳ�����
}
result = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public void closeDB(){
try {
if(result != null){
result.close();
result = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("�ر����ݿ�ʧ��");
}
}
}
最近下载更多
qwqwx585160 LV2
2024年12月15日
bankroll LV5
2024年6月13日
asddwh LV13
2023年12月29日
mengmeng12323423 LV4
2023年6月17日
lwlwlwlw LV3
2023年6月14日
chen_jw LV10
2023年2月17日
include LV8
2022年12月30日
13169178967 LV2
2022年12月11日
傻瓜还没打死烦人 LV7
2022年12月6日
zhunishimian LV6
2022年10月30日

最近浏览