问题描述
packagevisual;importjava.awt.*;importjavax.swing.*;importjava.sql.*;importjava.awt.event.*;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassloginextendsJFrameimplementsActionListener{//Connectioncon=null;ResultSetrs=null;Statementstmt=null;//PreparedStatementps=null;Stringsql;privateJTextFieldtextField;privateJTextFieldtextField_1;privateJPasswordFieldpasswordField1;privateJButtonbutton;privateJButtonbutton_1;publiclogin(){setTitle("u5B66u751Fu767Bu9646u754Cu9762");getContentPane().setLayout(null);JLabellabel=newJLabel("u5B66u53F7uFF1A");label.setBounds(111,39,55,20);getContentPane().add(label);textField=newJTextField();textField.setBounds(176,38,137,20);getContentPane().add(textField);textField.setColumns(10);JLabellabel_1=newJLabel("u59D3u540DuFF1A");label_1.setBounds(111,69,55,18);getContentPane().add(label_1);textField_1=newJTextField();textField_1.setBounds(176,66,137,21);getContentPane().add(textField_1);textField_1.setColumns(10);JLabellabel_2=newJLabel("u5BC6u7801uFF1A");label_2.setBounds(111,100,55,18);getContentPane().add(label_2);passwordField1=newJPasswordField(10);passwordField1.setBounds(176,97,137,21);getContentPane().add(passwordField1);button=newJButton("u767Bu5F55");button.setBounds(176,128,66,21);button.addActionListener(this);getContentPane().add(button);button_1=newJButton("u64A4u56DE");button_1.setBounds(250,128,66,21);button_1.addActionListener(this);getContentPane().add(button_1);setSize(400,300);setVisible(true);}publicvoidactionPerformed(ActionEvente){StringstNo=textField.getText();Stringpsw=passwordField1.getText();sql="select学号,密码fromstudentwhere学号='"+stNo+"'and密码='"+psw+"'";try{connectionc=newconnection();stmt=c.getConnection().createStatement();System.out.println(stmt);//这里出不来,怎么破rs=stmt.executeQuery(sql);if(rs.next()){newAlreadylogin(stNo);this.setVisible(false);}elseJOptionPane.showMessageDialog(this,"用户名或密码错误");c.closeConnection();}catch(Exceptionex){}/*if(e.getActionCommand()=="u767Bu5F55"){if(textField.getText().equals("0001")&&textField_1.getText().equals("admin")){this.setVisible(false);newAlreadylogin();}else{JOptionPane.showMessageDialog(this,"用户名或密码错误");}}elseif(e.getSource()==button_1){this.setVisible(false);newAdmin1();}*/}publicstaticvoidmain(String[]args){//TODO自动生成方法存根newlogin();//re.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}/*privatevoidconnection(){try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch(ClassNotFoundExceptionex){ex.printStackTrace();}try{con=DriverManager.getConnection("jdbc:odbc:test");}catch(SQLExceptionex){ex.printStackTrace();}}*/}
解决方案
解决方案二:
Connection这个是接口,你的代码就不能正常运行。建议你按照规范编码,类名大写.......。
解决方案三:
connectionc=newconnection();stmt=c.getConnection().createStatement();System.out.println(stmt);//这里出不来,怎么破这个connection类的getConnection()这个方法怎么写的??如果连接不到数据库当调用c.getConnection()时应该就会报错了!不知道这个方法你是怎么写的?你说的“这里出不来,怎么破”什么意思??是null吗?