java.lang.NullPointerException at simpleusers.LoginFrame.jButton1_actionPerformed(LoginFrame.java:87) at simpleusers.LoginFrame_jButton1_actionAdapter.actionPerformed(LoginFrame.java:122) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)加载JDBC数据库驱动程序出错数据库联接出错 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245) at java.awt.Component.processMouseEvent(Component.java:5134) at java.awt.Component.processEvent(Component.java:4931) at java.awt.Container.processEvent(Container.java:1566) at java.awt.Component.dispatchEventImpl(Component.java:3639) at java.awt.Container.dispatchEventImpl(Container.java:1623) at java.awt.Component.dispatchEvent(Component.java:3480) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095) at java.awt.Container.dispatchEventImpl(Container.java:1609) at java.awt.Window.dispatchEventImpl(Window.java:1590) at java.awt.Component.dispatchEvent(Component.java:3480) at java.awt.EventQueue.dispatchEvent(EventQueue.java:450) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136) at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)我连接数据库的代码是
void jButton1_actionPerformed(ActionEvent e) {
String username=jT1.getText().trim();
String password=jP1.getText().trim();
String sql1="select*from userandpwd where username='"+username+"'";
String sql2="inser userandpwd(username,pwd)values('"+username+
"','"+password+"')";
String msg=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e1){
System.out.println("加载JDBC数据库驱动程序出错!!!");
}
String ur1="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Test";
try{
con=DriverManager.getConnection(ur1,"sa","");
stmt=con.createStatement();
rs=stmt.executeQuery(sql1);
}catch(SQLException e2){
System.out.println("数据库联接出错");
}
try{
if(rs.next())
msg="对不起你注册的用户已经存在"; else
stmt.executeUpdate(sql2);
msg="欢迎你登录用户管理系统!\n"+
"你注册的用户名称为"+rs.getString("username")+
".\n你注册的密码为"+rs.getString("password")+"."; }catch(SQLException e3){
e3.printStackTrace();
System.out.println("插入数据出错");
}
try{
rs.close();
stmt.close();
con.close();
}catch(SQLException e4){
System.out.println("关闭对象链接出错");
}
void jButton1_actionPerformed(ActionEvent e) {
String username=jT1.getText().trim();
String password=jP1.getText().trim();
String sql1="select*from userandpwd where username='"+username+"'";
String sql2="inser userandpwd(username,pwd)values('"+username+
"','"+password+"')";
String msg=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e1){
System.out.println("加载JDBC数据库驱动程序出错!!!");
}
String ur1="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Test";
try{
con=DriverManager.getConnection(ur1,"sa","");
stmt=con.createStatement();
rs=stmt.executeQuery(sql1);
}catch(SQLException e2){
System.out.println("数据库联接出错");
}
try{
if(rs.next())
msg="对不起你注册的用户已经存在"; else
stmt.executeUpdate(sql2);
msg="欢迎你登录用户管理系统!\n"+
"你注册的用户名称为"+rs.getString("username")+
".\n你注册的密码为"+rs.getString("password")+"."; }catch(SQLException e3){
e3.printStackTrace();
System.out.println("插入数据出错");
}
try{
rs.close();
stmt.close();
con.close();
}catch(SQLException e4){
System.out.println("关闭对象链接出错");
}
数据库联接出错 驱动包导入了没有
String sql2="inser userandpwd(username,pwd)values('"+username+
"','"+password+"')"; 有可能执行这两个sql抛出的异常吧.
sql2 你是写增加吗?好像inser 是insert 吧.insert into 表名 (列名,列名) values('','')sql1 select*from 用空格隔开.select * from
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=sample";而SQL Server 2005 中加载驱动和url的语句则为String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=sample";可以看出版本不同连接方法也是不同的。SQL Server有许多不同版本,看你用的是哪个了
这类异常直接关系到下面的处理,所以应该中断程序才对。
我的资源里有sqlserver的驱动包。