我的配置文件内容是:
DataSource=jdbc:sqlserver://192.168.1.0:1433; DatabaseName=test1
UserName=sa
Password= ***
Driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
我的代码是:
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class Sqlconn {
/**
* Creates a new instance of ConnFactory
*/
private static Connection dbConn = null;
private static boolean mesFlag;//用来标识是否已经显示了提示信息
private static String dataSource;
private static String driver;
private static String userName;
private static String password;
private static boolean inited = init();
private Sqlconn() {}
/**
* Description of the Method
*
* @return Description of the Return Value
*/
public static Connection createConn() {
try {
Class.forName(driver);
dbConn = DriverManager.getConnection(dataSource,userName,password);
return dbConn;
}catch(SQLException se){
if(mesFlag==false){
JOptionPane.showMessageDialog(null, "数据库访问出错,请检查数据库配置文件或者数据库本身!");
mesFlag=true;
}
return dbConn;
}
catch(NullPointerException npe){
return dbConn;
}
catch (Exception ex) {
ex.printStackTrace();
return dbConn;
}
}
public static Connection getConnection(){
if(dbConn == null)
dbConn = createConn();
return dbConn;
}
public static Statement getStatement(){
Statement stmt = null;
try{
stmt = getConnection().createStatement();
}catch(Exception ex){
ex.printStackTrace();
}
return stmt;
}
public static void closeDbConn(){
if(dbConn!=null){
try {
dbConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
dbConn=null;
}
} private static boolean init()
{
try
{
Properties properties = new Properties();
FileInputStream fileinputstream = new FileInputStream("src\\resource\\config\\DB.properties");
properties.load(fileinputstream);
fileinputstream.close();
dataSource = properties.getProperty("DataSource");
driver = properties.getProperty("Driver");
userName = properties.getProperty("UserName");
password = properties.getProperty("Password");
mesFlag=false;
}catch(FileNotFoundException fnfe){
JOptionPane.showMessageDialog(null,"没有找到注册应用数据库的配置文件,请检查!");
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
return true;
}
}
运行后出现的错误是
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at yinshua.Cp_denglu$1.actionPerformed(Cp_denglu.java:69)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
请问我的是什么错误??谢谢回答!!
DataSource=jdbc:sqlserver://192.168.1.0:1433; DatabaseName=test1
UserName=sa
Password= ***
Driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
我的代码是:
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class Sqlconn {
/**
* Creates a new instance of ConnFactory
*/
private static Connection dbConn = null;
private static boolean mesFlag;//用来标识是否已经显示了提示信息
private static String dataSource;
private static String driver;
private static String userName;
private static String password;
private static boolean inited = init();
private Sqlconn() {}
/**
* Description of the Method
*
* @return Description of the Return Value
*/
public static Connection createConn() {
try {
Class.forName(driver);
dbConn = DriverManager.getConnection(dataSource,userName,password);
return dbConn;
}catch(SQLException se){
if(mesFlag==false){
JOptionPane.showMessageDialog(null, "数据库访问出错,请检查数据库配置文件或者数据库本身!");
mesFlag=true;
}
return dbConn;
}
catch(NullPointerException npe){
return dbConn;
}
catch (Exception ex) {
ex.printStackTrace();
return dbConn;
}
}
public static Connection getConnection(){
if(dbConn == null)
dbConn = createConn();
return dbConn;
}
public static Statement getStatement(){
Statement stmt = null;
try{
stmt = getConnection().createStatement();
}catch(Exception ex){
ex.printStackTrace();
}
return stmt;
}
public static void closeDbConn(){
if(dbConn!=null){
try {
dbConn.close();
} catch (SQLException e) {
e.printStackTrace();
}
dbConn=null;
}
} private static boolean init()
{
try
{
Properties properties = new Properties();
FileInputStream fileinputstream = new FileInputStream("src\\resource\\config\\DB.properties");
properties.load(fileinputstream);
fileinputstream.close();
dataSource = properties.getProperty("DataSource");
driver = properties.getProperty("Driver");
userName = properties.getProperty("UserName");
password = properties.getProperty("Password");
mesFlag=false;
}catch(FileNotFoundException fnfe){
JOptionPane.showMessageDialog(null,"没有找到注册应用数据库的配置文件,请检查!");
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
return true;
}
}
运行后出现的错误是
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at yinshua.Cp_denglu$1.actionPerformed(Cp_denglu.java:69)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
请问我的是什么错误??谢谢回答!!
数据库驱动包没有导入吧!
at yinshua.Cp_denglu$1.actionPerformed(Cp_denglu.java:69)
应该是你的Cp_denglu.java:69行的引用了空指针,具体原因不明,有可能是你数据库驱动的问题,也有可能是你代码的问题。你的数据库是不是sql-server2000?
改成
Connection conn=Sqlconn.createConn();
Statement h1=conn.createStatement();
看看报什么错