首页 新闻 搜索 专区 学院

java中连接数据库出问题不知道怎么解决

0
悬赏园豆:10 [已解决问题] 解决于 2015-01-13 13:48
package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

import view.DengluWindow;
import view.VoteWindow;

import model.AllWin;

public class OKButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        //创建“VoteWindow窗体”
        //AllWin.voteWindow=new VoteWindow("这是投票的窗体!","tubiao.jpg","beijing.jpg");
        
       String name=DengluWindow.userField.getText();
        String mima=DengluWindow.passwordField.getText();
        Connection con1;
         Statement st;
         ResultSet rs;
         if(name.equals("")||mima.equals(""))//如果没输用户名或密码,则提示对不起,请输入用户名或密码 
         { 
             JOptionPane.showOptionDialog(null,"Error:用户名或者密码不能为空!","警告提示!", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null); 
         } 
         else//如果都有数据了就开始连接数据库验证 
         { 
             try    
           { 
             // 加载驱动程序
               Class.forName("com.mysql.jdbc.Driver"); 
               //建立连接
              con1=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/user","root","520520"); 
              st=(Statement) con1.createStatement(); 
              rs=((java.sql.Statement) st).executeQuery("select * from tb_user where username='"+name+"'"); 
                 if(rs.next())//如果存在,就验证密码 
                 { 
                     if(rs.getString("pwd").equals(mima))//如果密码正确就提示,反之 
                     {  
                        //隐藏登录窗体
                         AllWin.dengluWindow.setVisible(false);
                         AllWin.dengluWindow.validate();

                
                        //创建“VoteWindow窗体”
                         AllWin.voteWindow=new VoteWindow("这是投票的窗体!","tubiao.jpg","beijing.jpg");
                         
                         
                     } 
                  
                     else 
                     { 
                         JOptionPane.showOptionDialog(null,"Error:密码错误!","警告提示!", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null); 
                     } 
                 } 
              
                 else//如果没有查找到用户名就提示 
                 { 
                     JOptionPane.showOptionDialog(null,"Error:没有此用户名!","警告提示!", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,null,null); 
                 } 
            
           
        } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        
        }
         

    }
    

}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at controller.OKButtonListener.actionPerformed(OKButtonListener.java:26)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6267)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6032)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

就是有两个窗体,登陆和投票,登陆时有用户名和密码,点击提交按钮就进入到投票窗体。按钮有监听,就是这个监听有问题。我在数据库里建立了一张用户表,只要符合表里的用户名和密码就能登陆进入投票窗体。

唐小密的主页 唐小密 | 初学一级 | 园豆:181
提问于:2015-01-09 19:33
< >
分享
最佳答案
0

出错信息是空指针异常,行号为26行,看你的代码极有可能出问题的地方在

//建立连接

con1=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/user","root","520520");

st=(Statement) con1.createStatement();

rs=((java.sql.Statement) st).executeQuery("select * from tb_user where username='"+name+"'");

可断点检查con1,st,rs是否取到的为空

 是否为数据库连接错误,getConnection("jdbc:mysql://localhost/user","root","520520");

收获园豆:10
编码的艺术 | 菜鸟二级 |园豆:218 | 2015-01-12 14:05

谢谢啦

唐小密 | 园豆:181 (初学一级) | 2015-01-13 13:49
其他回答(1)
0

有个变量没有实例化或者取到的为空,在Eclipse Debug单步可直接调试

2012 | 园豆:20730 (高人七级) | 2015-01-10 18:00

谢谢啦

支持(0) 反对(0) 唐小密 | 园豆:181 (初学一级) | 2015-01-13 13:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册