代码如下:
package util;import java.sql.*;/**
* 单例式数据库连接模式
*
*
*/
public abstract class DBConnection{
private static Connection conn=null; /**
* 与数据库建立连接
*
* 返回值-Connection对象
*/
public static Connection getConnection(){
try{
if(conn == null){
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=InfoSite", "xjfy","lgfree2006");
//Class.forName("com.mysql.jdbc.Driver");
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root","");
Debug.log("Connecion created.");
}else{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM admin");
if(rs==null||!rs.next()){
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=InfoSite", "xjfy","lgfree2006");
//Class.forName("com.mysql.jdbc.Driver");
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root","root");
Debug.log("Connecion re-created.");
} }
}
catch(Exception ex){
Debug.log(Debug.getExceptionMsg(ex));
}
finally{
return conn;
}
}
public static void main(String[] args){
try{
Connection co=DBConnection.getConnection();
Statement stmt=co.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM admin");
if (rs.next()){
System.out.println("connected");
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
错误提示:Exception in thread "main" java.lang.NullPointerException
at util.DBConnection.main(DBConnection.java:53)
环境:数据库环境配置没有问题。不使用单例模式,连接数据库正常。高手帮忙!!!!
package util;import java.sql.*;/**
* 单例式数据库连接模式
*
*
*/
public abstract class DBConnection{
private static Connection conn=null; /**
* 与数据库建立连接
*
* 返回值-Connection对象
*/
public static Connection getConnection(){
try{
if(conn == null){
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=InfoSite", "xjfy","lgfree2006");
//Class.forName("com.mysql.jdbc.Driver");
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root","");
Debug.log("Connecion created.");
}else{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM admin");
if(rs==null||!rs.next()){
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=InfoSite", "xjfy","lgfree2006");
//Class.forName("com.mysql.jdbc.Driver");
//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root","root");
Debug.log("Connecion re-created.");
} }
}
catch(Exception ex){
Debug.log(Debug.getExceptionMsg(ex));
}
finally{
return conn;
}
}
public static void main(String[] args){
try{
Connection co=DBConnection.getConnection();
Statement stmt=co.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM admin");
if (rs.next()){
System.out.println("connected");
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
错误提示:Exception in thread "main" java.lang.NullPointerException
at util.DBConnection.main(DBConnection.java:53)
环境:数据库环境配置没有问题。不使用单例模式,连接数据库正常。高手帮忙!!!!
解决方案 »
- java 正则表达式获取值
- [紧急求助] JAVA 3DES加密问题
- 注册监听器为什么失效?
- 我的Eclipse总是显示代码有问题,明明300行的,只显示20行,啥原因 重下eclipse,老样子 请问有办法吗 谢谢
- 这个过程那里错了啊..请高手来帮帮.!!
- 有没有这样的涂鸦板的代码
- 请问:什么是组合?看了THINGKING IN JAVA后,还是不是很明白
- 请问批处理文件怎么写?
- 为什么我用jbuilder6.0写的程序,在运行时中文汉字显示不出来?
- Unsafe里putInt(),putIntVolatile()和putOrderedInt()有什么区别?
- 高分求解决两个问题
- 一个数组的问题
不明白你的else部分为什么还要再重新生成Connection,本来是conn != null才会进入else语句块,这里直接返回conn应该就可以了。至于错误,提示里说的很清楚,53行的NullPointerException,设置断点调试一下就会发现是哪里出问题了。