public?class?DBConnUtil?{
????private?static?final?String?URL="jdbc:mysql://192.168.0.105:3306/imooc";
????private?static?final?String?USER="zabbix";
????private?static?final?String?PASSWORD="zabbix";
????
????//聲明一個(gè)靜態(tài)變量
????private?static?Connection?conn?=?null;
????
????//放到靜態(tài)塊中
????static?{
????????try?{
????????????//加載驅(qū)動(dòng)
????????????Class.forName("com.mysql.jdbc.Driver");
????????????
????????????conn?=?DriverManager.getConnection(URL,USER,PASSWORD);
????????}?catch?(ClassNotFoundException?e)?{
????????????e.printStackTrace();
????????}?catch?(SQLException?e)?{
????????????e.printStackTrace();
????????}
????}
????
????public?static?Connection?getConn(){
????????return?conn;
????}
}我在每次使用完數(shù)據(jù)庫(kù)后會(huì)關(guān)掉這個(gè)連接(按道理應(yīng)該關(guān)閉吧)conn.close();然后第二次操作數(shù)據(jù)庫(kù)就No operations allowed after connection closed.這個(gè)問(wèn)題要怎么解決?總不能真的注銷 conn.close(); 吧
添加回答
舉報(bào)
0/150
提交
取消