数据库的连接怎么断不了呢! 一个简单的用户验证,验证完了就关闭数据库,但出错java.lang.NullPointerException,错在Connection的close()方法上,怎么回事啊!郁闷了!, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java.lang.NullPointerException,不是关不掉,而是connection就是null。 要关闭前先做个判断 try{ if(Connection != null){ Connection.close(); } }catch(Exception e){ ^.....} 我关于数据库的基本操作是放在一个类中的,类的成员方法都是静态的啊。代码如下: import java.sql.*;public class cardConnect{ public cardConnect() { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); } catch (ClassNotFoundException ex) { } try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } public static Connection getconn() throws SQLException { String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JBbuilder"; String username="jbuilder"; String password="jbuilder"; return DriverManager.getConnection(url,username,password); } public static void close(ResultSet rs) { try { rs.close(); } catch (SQLException ex) { } } public static void close(Statement stmt) { try { stmt.close(); } catch (SQLException ex) { } } public static void close(Connection conn) { try { conn.close(); } catch (SQLException ex) { } } private static cardConnect dbconn=new cardConnect(); private void jbInit() throws Exception { }}这是我在另外一个文件建立对数据库的连接: Connection con=cardConnect.getconn(); //不用建立对象,直接调用cardConnect的方法关闭数据库: cardConnect.close(con);到底错在哪啊? Connection con=cardConnect.getconn(); 判con是不是null 你的Connection应该没有实例化起来,最好不要使用静态的声明.封装在一个类里会好点.public class mainDataModule { private Connection dbConn; public mainDataModule() { try { jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } private void jbInit() throws Exception{ dbConn = getDatabaseConnection(); dbConn.setAutoCommit(false); } private Connection getDatabaseConnection() { String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl817"; String dbdriver = "oracle.jdbc.driver.OracleDriver"; String userid = null; String passwd = null; Connection conn = null; try { Properties dbproperty = getProperties("cmas_system.properties"); url = (String)dbproperty.get("DATABASE_URL"); dbdriver = (String)dbproperty.get("DRIVER_CLASS"); userid = (String)dbproperty.get("USER_NAME"); passwd = (String)dbproperty.get("PASSWORD"); Class.forName(dbdriver); conn = DriverManager.getConnection(url, userid, passwd); conn.setAutoCommit(false); } catch(Exception ex) { ex.printStackTrace(); } return conn; } superjames(均) 的对google,摆渡找下像这样的例子很多,不要自己去写了,向你这样也不方便别人使用如果在页面上使用rs 要关闭,使用stmt也要关闭,太累了,代码要写很多,也很烦;不实用,现在采用的技术是.hibernate,一定意义上已经脱离了对jdbc编程,建议去看下,这些技术也是将来2,3年的发展方向你可以去看看这两个http://community.csdn.net/Expert/topic/4055/4055407.xml?temp=.6280176http://community.csdn.net/Expert/topic/4057/4057140.xml?temp=.6880457 求教一个eclipse使用中的关于泛型的问题 jxl读取excel四舍五入问题 新手的一系列问题.请大侠HELP! ----关于获得焦点的问题---- 【求复利计算】 通过response重定向页面的参数值乱码问题。 swing的问题,谢谢大家 为什么加上package就不行? 做程序员还不如去开个店子卖点烟酒零食算了,郁闷,送分,送完为止(5) 送分!!! 调试时为何无法跟踪入方法内部? LayoutManager的问题!(诸位兄台,大家捧捧场吧!) 请教关于JConnect连接Sybase数据库的问题
try{
if(Connection != null){
Connection.close();
}
}catch(Exception e){
^.....
}
代码如下:
import java.sql.*;public class cardConnect
{
public cardConnect()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch (ClassNotFoundException ex)
{
}
try
{
jbInit();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public static Connection getconn() throws SQLException
{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JBbuilder";
String username="jbuilder";
String password="jbuilder";
return DriverManager.getConnection(url,username,password);
}
public static void close(ResultSet rs)
{
try
{
rs.close();
}
catch (SQLException ex)
{
}
}
public static void close(Statement stmt)
{
try
{
stmt.close();
}
catch (SQLException ex)
{
}
}
public static void close(Connection conn)
{
try
{
conn.close();
}
catch (SQLException ex)
{
}
}
private static cardConnect dbconn=new cardConnect();
private void jbInit()
throws Exception
{
}
}这是我在另外一个文件建立对数据库的连接:
Connection con=cardConnect.getconn(); //不用建立对象,直接调用cardConnect的方法
关闭数据库:
cardConnect.close(con);
到底错在哪啊?
判con是不是null
public class mainDataModule {
private Connection dbConn; public mainDataModule() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
} private void jbInit() throws Exception{
dbConn = getDatabaseConnection();
dbConn.setAutoCommit(false);
} private Connection getDatabaseConnection() {
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl817";
String dbdriver = "oracle.jdbc.driver.OracleDriver";
String userid = null;
String passwd = null;
Connection conn = null; try
{
Properties dbproperty = getProperties("cmas_system.properties");
url = (String)dbproperty.get("DATABASE_URL");
dbdriver = (String)dbproperty.get("DRIVER_CLASS");
userid = (String)dbproperty.get("USER_NAME");
passwd = (String)dbproperty.get("PASSWORD"); Class.forName(dbdriver);
conn = DriverManager.getConnection(url, userid, passwd);
conn.setAutoCommit(false);
}
catch(Exception ex) {
ex.printStackTrace();
} return conn;
}
你可以去看看这两个
http://community.csdn.net/Expert/topic/4055/4055407.xml?temp=.6280176
http://community.csdn.net/Expert/topic/4057/4057140.xml?temp=.6880457