学生管理系统登录界面的代码:
package yh;
import java.sql.SQLException;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class SelectFrame extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int LEFT = 0;
private JLabel yonghu=new JLabel("用户: ");
private JTextField text_name=new JTextField(10);
private JLabel mima=new JLabel("密码: ");
private JTextField text_mima=new JTextField(10);
private JButton but_ok=new JButton("确定");
private JButton but_exit=new JButton("取消");
private JPanel pan1=new JPanel();
private JPanel pan2=new JPanel();
private JPanel pan3=new JPanel();
/*private JPanel jp=new JPanel();*/
public SelectFrame()
{
this.setTitle("学生管理系统登录");
setSize(300,350);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
/*jp.setLayout(new FlowLayout()); //设置面板布局
ImageIcon jp=new ImageIcon(getClass().getResource("/Img/天空的云.jpg"));
this.getLayeredPane().add(jp);*/
setVisible(true);
add(pan1);
add(pan2);
add(pan3);
String path="/Img/Java学校校徽图标.jpg";
try {
Image img=ImageIO.read(this.getClass().getResource(path));
this.setIconImage(img);
}
catch(IOException e){
e.printStackTrace();
}
}
public void init()
{
pan1.add(yonghu);
pan1.add(text_name);
pan2.add(mima);
pan2.add(text_mima);
pan3.add(but_ok);
pan3.add(but_exit);
setLayout(new FlowLayout(LEFT));
but_ok.addActionListener(this);
but_exit.addActionListener(this);
setLayout(new GridLayout(3,1));
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Connection conn;
PreparedStatement pt;
ResultSet rs1;
// String url = "jdbc:mysql://localhost:3306/tt";
if(e.getSource()==but_ok)
{
String a=text_name.getText();
String b=text_mima.getText();
if(a.equals("")||b.equals(""))
{
JOptionPane.showMessageDialog(null, "请输入用户名或密码");
}
else
{
try {
// 连接数据库(安装的数据库为MYSQL)
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tt?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "root");
//3306端口号,tt为我创建数据库的名字
//指定字符的编码、解码格式。编码格式要一致,为utf-8,自己可以设置eclipse的默认编码格式;
// 建立Statement对象
// stmt = conn.createStatement();
String sql="select * from 用户信息表 where username=?";
pt=conn.prepareStatement(sql);
pt.setString(1, a);
// pt.setString(2, b);
/**
* Statement createStatement() 创建一个 Statement 对象来将 SQL 语句发送到数据库。
*/
// 执行数据库查询语句
rs1 = pt.executeQuery();
/**
* ResultSet executeQuery(String sql) throws SQLException 执行给定的 SQL
* 语句,该语句返回单个 ResultSet 对象
*/
if(rs1.next()) //实现连接,与数据库信息匹配
{
String d=rs1.getString(1).trim();
String f=rs1.getString(2).trim();
if(a.equals(d)&&b.equals(f))
{
sql S=new sql();
S.init1();
//JOptionPane.showMessageDialog(null, "欢迎您!"+d);
//this.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "密码错误,登录失败");
//this.dispose();
}
}
else
{
JOptionPane.showMessageDialog(null, "用户名不存在");
//this.dispose();
}
rs1.close();
pt.close();
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
else
if(e.getSource()==but_exit) //关闭窗体
{
System.exit(0);
}
}
//public static void main(String[] args) {
//
new SelectFrame();
// }
}