java吧 关注:1,264,477贴子:12,765,155
  • 2回复贴,共1

哪儿高手分析下,怎么就连不上数据库呢

只看楼主收藏回复

package MPSMS;
package MPSMS;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
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.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class load extends JFrame implements ActionListener
 {
  JPanel  jp1=new JPanel();
  JLabel  label1=new JLabel("账号:");
  
  JTextField name=new JTextField();
  JLabel  label2=new JLabel("密码:");
  JPasswordField password=new JPasswordField();
  JButton inBbtn=new JButton("进入");
  JButton outbtn=new JButton("退出");
  
  JTextField text1=new JTextField();
  JTextField text2=new JTextField();
  load()
     {   // setVisible(true);
         // setSize(300,250);
    
     
        this.add(jp1);
        jp1.setLayout(null);
        jp1.add(inBbtn);
        jp1.add(outbtn);
        jp1.add(label1);
       jp1.add(label2);
       jp1.add(name);
       jp1.add(password);
       name.setBounds(100,40,140,20);
       password.setBounds(100,80,140,20);
       label1.setBounds(60,40,70,20);
       label2.setBounds(60,80,70,20);
       inBbtn.setBounds(60,120,70,20);
       outbtn.setBounds(180,120,70,20);
        this.setTitle("登陆");
        this.setBounds(340,240,300,200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);



IP属地:浙江1楼2009-07-27 09:56回复
           inBbtn.addActionListener(this);
           outbtn.addActionListener(this);
         }
       public static void main(String[] args)
       {
       load load1=new load();
       
       }
       
       
       
      public Boolean yanzheng(String name,String pswd) 
      { 
      Boolean has=false;//判断用户是否存在 
      Connection con; 
      Statement sql; 
      ResultSet rs; 
      try 
      { 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
      } 
      catch(ClassNotFoundException e) 
      {} 
      try{ 
      con=DriverManager.getConnection("jdbc:odbc:UserId"); 
      sql=con.createStatement(); 
      rs=sql.executeQuery("Select * FROM uers"); 
      while(rs.next()) 
      { 
      if((name.equals(rs.getString("username")))&&(pswd.equals(rs.getString("password"))) )
      {has=true;} 
      } 
      con.close(); 
      } 
      catch(SQLException el) 
      {} 
      return has; 
      } 
      
      public void actionPerformed(ActionEvent f) {
       // TODO Auto-generated method stub
       if(f.getSource()==outbtn){
        this.setVisible(false);
       }
       else if(f.getSource()==inBbtn){
        if(this.yanzheng(name.getText().trim(), password.getText().trim()))
        {
         new ygguanli();
        }
        else 
        {JOptionPane.showMessageDialog(this,"用户或者密码不存在!");} 
        
       }
      }}
    数据源已经配好了,数据库是ACESS的名字叫UserId,就一个uers表,两个字段username和password


    IP属地:浙江2楼2009-07-27 09:56
    回复