该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*; import javax.swing.*;
public class LoginSystem extends JFrame{
public LoginSystem(){
JLabel label=null;
this.getContentPane().add(label=new JLabel("用户名:")).setBounds(10, 10, 100,30 );
this.getContentPane().add(label=new JLabel("密 码:")).setBounds(10, 50, 100, 30);
final JTextField jf=new JTextField();
final JPasswordField jp=new JPasswordField();
this.getContentPane().add(jf).setBounds(120, 10, 100, 30);
this.getContentPane().add(jp).setBounds(120, 50, 100, 30);
final JButton jb=new JButton("登录");
this.getContentPane().add(jb).setBounds(10, 90, 100, 30);
this.setLayout(null);
this.setBounds(100, 100, 300, 200);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String use=jf.getText();
String pwd=jp.getText();
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://127.0.0.1:3306/student";
String user="root";
String password="root";
try {
Class.forName(driver);
Connection con=DriverManager.getConnection(url,user,password);
Statement state=con.createStatement();
ResultSet rs=state.executeQuery("select *from login where user='"+use+"'");
if(rs.next()){
if(rs.getString("password").equals(pwd)){
JOptionPane.showMessageDialog(jb, "登录成功!");
}
else
JOptionPane.showMessageDialog(jb, "登录失败!");
}
else
JOptionPane.showMessageDialog(jb, "登录失败!");
} catch (Exception e2) {
// TODO: handle exception
}
}
});
}
public static void main(String[] args) {
LoginSystem ls=new LoginSystem();
ls.setVisible(true);
} }
小弟也是初学~~~希望大家别笑话哦。。。。