jframe和mysql登陆_刚写的一个从数据库读取账户和密码进行登陆的小程序~高手请无~...
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
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);
} }
小弟也是初學~~~希望大家別笑話哦。。。。
總結
以上是生活随笔為你收集整理的jframe和mysql登陆_刚写的一个从数据库读取账户和密码进行登陆的小程序~高手请无~...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java hashmap实例_java
- 下一篇: mycat mysql 物理部署_一、M