如何关闭数据库连接?

Iris5 2010-04-19 10:51:31
如何关闭数据库连接?

以下程序是一个片段,整个程序能够从excel文件中读取数据并插入数据库中。但是整个程序执行完毕后如何关闭数据库连接?请给出详细代码、并告知此代码放置何处?

补充:目前的状况是当读取excel多个值的时候,向数据库插入完毕,例如读取插5个值,mysql数据库会显示5个数据库连接(未关闭),如何使插值结束,同时关闭数据库连接?谢谢!!


package d123;


public class POIDZ {
public static Connection conn = null;
public static Statement stmt = null;
public static boolean connectDB2() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost:3306/dstore";
conn = DriverManager.getConnection(url,"root","123");
stmt = conn.createStatement();
}
//捕获加载驱动程序异常
catch (ClassNotFoundException cnfex) {
cnfex.printStackTrace();
return false;
}
//捕获连接数据库异常
catch (SQLException sqlex) {

sqlex.printStackTrace();
//System.exit(1); // terminate program
return false;
}
return true;
}
public static boolean readExcelToDB2(String userName,String fahao) {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
FileInputStream fjexl = null;
try {
fjexl = new FileInputStream("D:\\Tomcat6.0\\webapps\\aas\\upload\\"+userName+".xls");
fs = new POIFSFileSystem(fjexl);
wb = new HSSFWorkbook(fs);
fjexl.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;

String shoudaot ="8";
long jieshoutimet =7;
int rowNum, cellNum;
int i;
rowNum = sheet.getLastRowNum();

for (i = 3; i <= 30; i++) { ---->第一个循环
row = sheet.getRow(i);
//cellNum = row.getLastCellNum();
try{
cell = row.getCell((short) 10);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
dongci = cell.getStringCellValue().trim();
dongci=new String(dongci.getBytes("GBK"),"latin1");
}catch(Exception e){
}
if (haomat!=0 & jieshou!="" & shengdan!=""){
String sql = "insert into dingzhi(user,name) values('"+userName+"','"+namet+"')";
try {
stmt.executeUpdate(sql);
shengdan="";
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
}
}
rowNum = sheet.getLastRowNum();

for (i = 3; i <= 30; i++) { ----》第二个循环
row = sheet.getRow(i);
//cellNum = row.getLastCellNum();
try{
cell = row.getCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
name = cell.getStringCellValue().trim();
name=new String(name.getBytes("GBK"),"latin1");
name = name.substring(0, 20);
}catch(Exception e){

}
if (name!="-"){
String sql = "insert into listuser(username,liantong) values('"+name+"','"+liantong+"')";
try {
stmt.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
}
}
return true;

}
}
...全文
203 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
woaiyanzic 2010-04-19
  • 打赏
  • 举报
回复

package d123;

import java.sql.Connection;
import java.sql.Statement;

public class POIDZ {
public static Connection conn = null;
public static Statement stmt = null;

public static boolean connectDB2() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost:3306/dstore";
conn = DriverManager.getConnection(url, "root", "123");
stmt = conn.createStatement();
}
// 捕获加载驱动程序异常
catch (ClassNotFoundException cnfex) {
cnfex.printStackTrace();
return false;
}
// 捕获连接数据库异常
catch (SQLException sqlex) {

sqlex.printStackTrace();
// System.exit(1); // terminate program
return false;
}
return true;
}

public static boolean readExcelToDB2(String userName,String fahao) {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
FileInputStream fjexl = null;
try {
fjexl = new FileInputStream("D:\\Tomcat6.0\\webapps\\aas\\upload\\"+userName+".xls");
fs = new POIFSFileSystem(fjexl);
wb = new HSSFWorkbook(fs);
fjexl.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;

String shoudaot ="8";
long jieshoutimet =7;
int rowNum, cellNum;
int i;
rowNum = sheet.getLastRowNum();

for (i = 3; i <= 30; i++) { ---->第一个循环
row = sheet.getRow(i);
// cellNum = row.getLastCellNum();
try{
cell = row.getCell((short) 10);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
dongci = cell.getStringCellValue().trim();
dongci=new String(dongci.getBytes("GBK"),"latin1");
}catch(Exception e){
}
if (haomat!=0 & jieshou!="" & shengdan!=""){
String sql = "insert into dingzhi(user,name) values('"+userName+"','"+namet+"')";
try {
stmt.executeUpdate(sql);
shengdan="";
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
stmt.close();
conn.close();
}
}
rowNum = sheet.getLastRowNum();

for (i = 3; i <= 30; i++) { ----》第二个循环
row = sheet.getRow(i);
// cellNum = row.getLastCellNum();
try{
cell = row.getCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
name = cell.getStringCellValue().trim();
name=new String(name.getBytes("GBK"),"latin1");
name = name.substring(0, 20);
}catch(Exception e){

}
if (name!="-"){
String sql = "insert into listuser(username,liantong) values('"+name+"','"+liantong+"')";
try {
stmt.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
stmt.close();
conn.close();
}
}
return true;

}
}

这个代码格式真成问题看着很累啊
weberwong 2010-04-19
  • 打赏
  • 举报
回复
没找到你在哪调用的connectDB2(),在调用方的最后关闭连接,一般打开和关闭配对用就可以了
Iris5 2010-04-19
  • 打赏
  • 举报
回复
我对数据库操作不熟悉,能否把代码放在整个程序中?非常感谢!
zhouxuegangjava163 2010-04-19
  • 打赏
  • 举报
回复
你好,楼主!
stmt.close();
conn.close();
woaiyanzic 2010-04-19
  • 打赏
  • 举报
回复
帅哥~~CONN和STMT都是要管的,否则你每次都NEW一个,然后就~~~关掉连接、流啊什么的都是用其close()方法你可以看看相应的API这是个好习惯,其实这个问题看了API都不用发帖子的~~
gao512008 2010-04-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 weberwong 的回复:]

conn.close()就关了,在你程序用完连接后调用,最好放在finally里
[/Quote]UP
weberwong 2010-04-19
  • 打赏
  • 举报
回复
conn.close()就关了,在你程序用完连接后调用,最好放在finally里

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧