java写的用户登陆实例,用eclipse开发的具体步奏和代码

2022-03-23 科技 93阅读
importjava.awt.Container;
importjava.awt.Font;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;importjavax.swing.*;publicclassLoginextendsJFrame{
JLabeluser,passwd;
JTextFielduserput;
JPasswordFieldpassput;
JButtondenglu,tuichu;
publicLogin(){
super("用户登录");
Containerc=getContentPane();
c.setLayout(null);

Fontf=newFont("宋体",Font.PLAIN,12);
user=newJLabel("账号");
passwd=newJLabel("密码");

userput=newJTextField();
passput=newJPasswordField();

denglu=newJButton("登录");
denglu.setFont(f);
denglu.addActionListener(newNewAction());
tuichu=newJButton("退出");
tuichu.setFont(f);
tuichu.addActionListener(newNewAction());

user.setBounds(50,50,60,20);
userput.setBounds(110,50,150,20);

passwd.setBounds(50,80,60,20);
passput.setBounds(110,80,150,20);

denglu.setBounds(50,160,60,30);
tuichu.setBounds(200,160,60,30);
c.add(user);
c.add(userput);
c.add(passwd);
c.add(passput);
c.add(denglu);
c.add(tuichu);

setSize(350,300);
setVisible(true);

}

classNewActionimplementsActionListener{
Stringurl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CDM";
Stringuser="sa";
Stringpasswd="394513265";
java.sql.Connectioncon;

publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection(url,user,passwd);
}catch(Exceptionep){
JOptionPane.showMessageDialog(null,"加载驱动失败!");
}

if(e.getSource()==denglu){
Find();
}
if(e.getSource()==tuichu){
dispose();
}
}publicvoidFind(){

Stringlk="select*fromlogin";
try{
Statementsql=con.createStatement();
ResultSetrs=sql.executeQuery(lk);
while(rs.next()){

if(rs.getString(1).equals(userput.getText())&&rs.getString(2).equals(passput.getText()))
newMainClient();
else
JOptionPane.showMessageDialog(null,"用户名或密码错误");

}

rs.close();
}catch(SQLExceptionp){
JOptionPane.showMessageDialog(null,p.getMessage());
}

}
}
publicstaticvoidmain(String[]args){
Loginl=newLogin();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
步骤就是建个工程然后建个class
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com