设计Swing
package cn.lab.JDBC;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login {
// public static void main(String[] args) {
// Login demo = new Login();
// demo.initUI();
// }
void initUI() {
//public static void main(String[] args) {
JFrame f = new JFrame("学生管理登录页面");
Container cp = f.getContentPane();
f.setSize(430, 220);
//f.setLocation(450,300);
f.setLocationRelativeTo(null);
f.setLayout(null);
JLabel labelUsername = new JLabel("账号: ");
labelUsername.setFont(new Font("楷体", Font.PLAIN, 20));
JTextField tfUsername = new JTextField(10);
JLabel labelPassword = new JLabel("密码: ");
labelPassword.setFont(new Font("楷体", Font.PLAIN, 20));
JTextField tfPassword = new JTextField(10);
JButton j1 = new JButton("注册");
JButton j2 = new JButton("登录");
JButton j3 = new JButton("退出");
labelUsername.setBounds(60, 5, 120, 70);
tfUsername.setBounds(110, 25, 260, 30);
labelPassword.setBounds(60, 40, 120, 70);
tfPassword.setBounds(110, 60, 260, 30);
j1.setBounds(50, 100, 60, 40);
j2.setBounds(190, 100, 60, 40);
j3.setBounds(330, 100, 60, 40);
cp.add(labelUsername);
cp.add(tfUsername);
cp.add(labelPassword);
cp.add(tfPassword);
cp.add(j1);
cp.add(j2);
cp.add(j3);
j1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (!new StuMS().login(tfUsername.getText(), tfPassword.getText())) {
try {
new StuMS().insert(tfUsername.getText(), tfPassword.getText());
} catch (Exception exception) {
exception.printStackTrace();
}
}
else{
JOptionPane.showMessageDialog(null, "id已存在,请登录", "注意", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
j2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// JButton JB = (JButton)e.getSource();
String id = tfUsername.getText();
String password = tfPassword.getText();
boolean out = false;
try {
out = new StuMS().login(id, password);
} catch (Exception exception) {
exception.printStackTrace();
}
if (out == true) {
Component frame = null;
JOptionPane.showInternalMessageDialog(frame, "已存在,登录成功",
"提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "id或密码错误,请重新检查", "注意", JOptionPane.ERROR_MESSAGE);
}
//labelPassword.setText(id);
}
});
j3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
//}
}
}
实现数据库链接
package cn.lab.JDBC;
import com.sun.tools.javac.Main;
import java.sql.*;
//1.导入驱动jar包
//2.注册驱动
//3.获取数据库连接对象
//4.定义sql 语句
//5.获取执行sql的对象Statement
public class StuMS {
Connection conn = null;
PreparedStatement pstml = null;
ResultSet rs = null;
public StuMS() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/test??useSSL=false", "root", "root");
}
public void insert(String username, String password) throws Exception {
String sql = "insert into infor(id,password) values (?,?);";
pstml = conn.prepareStatement(sql);
pstml.setString(1, username);
pstml.setString(2, password);
pstml.execute();
}
public boolean login(String username, String password) {
if (username == null || password == null) {
return false;
}
try {
String sql = "select * from infor where id=? and password= ? ";
pstml = conn.prepareStatement(sql);
pstml.setString(1, username);
pstml.setString(2, password);
rs = pstml.executeQuery();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstml != null) {
try {
pstml.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
}
最后是主程序调用
package cn.lab.JDBC;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Login demo = new Login();
demo.initUI();
// Scanner sc=new Scanner(System.in);
// System.out.println("请输入用户名:");
// String username=sc.next();
// System.out.println("请输入你的密码:");
// String password=sc.next();
// StuMS logain=new StuMS();
// boolean flag=StuMS.login(username,password);
// if(flag){
// System.out.println("登陆成功");
// }else {
// System.out.println("用户名或密码错误");
// }
}
}
实现样式
欢迎留言!