展开全部
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class Login extends JFrame implements ActionListener {
private JLabel lbId, lbPass;
private JTextField txtId, txtPass;
private JButton btLogin, btExit;
private Connection datacon = null;//连接对象
private PreparedStatement pstat = null;//命令对象
private ResultSet rs = null;//结果集对象
private JPanel pan;
public Login(){
e68a843231313335323631343130323136353331333264643130
pan=new JPanel();
lbId = new JLabel("账户名");
lbPass = new JLabel("密码");
txtId = new JTextField(10);
txtPass = new JTextField(10);
btLogin = new JButton("Login");
btExit = new JButton("Exit");
btLogin.addActionListener(this);
btExit.addActionListener(this);
pan.add(lbId);
pan.add(txtId);
pan.add(lbPass);
pan.add(txtPass);
pan.add(btLogin);
pan.add(btExit);
this.getContentPane().add(pan);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.pack();
this.setSize(520,350);
this.setVisible(true);
}
public void actionPerformed(ActionEvent evt){
if (evt.getSource().equals(btExit)) {
System.exit(0);
}
if(evt.getSource().equals(btLogin)){
try{
datacon = StarConnection.getConnection();
String str = "select * from Users where Id=? and Pass=?";//这里的*可以是这张表的某一个字段名。也可以是这张表的多个字段名,用逗号隔开。
//where后是查询条件。。。。。where 字段名 like ‘王*’;//这个是查姓王的条件方法like
pstat = datacon.prepareStatement(str);
pstat.setString(1, txtId.getText());
pstat.setString(2,txtPass.getText());
rs = pstat.executeQuery();
if(rs.next()){
txtId.setText("");
txtPass.setText("");
JFrame frm = new JFrame();
frm.add(new Score());
frm.setVisible(true);
frm.setSize(520, 350);
this.setVisible(false);
}
else
{
JOptionPane.showMessageDialog(null,"用户名或密码有错误!","测试窗口",JOptionPane.PLAIN_MESSAGE);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
/**
* filename:MyConnection.java
* 实现与数据库的连接,关闭操作
* @author Star
*
*/
import java.sql.*;
public class StarConnection {
//======getConnection方法,创建连接对象
public static Connection getConnection(){
Connection con = null;//声明数据库连接对象
try{
//引入桥接连接方法
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//创建连接对象
con=DriverManager.getConnection("jdbc:odbc:STAR");
}catch(Exception err){
err.printStackTrace();
}
return con;//返回连接对象
}
//===closeConnection方法,关闭数据库连接的对象
public static void closeConnection(Connection dbCon){
try{
if(dbCon !=null && !dbCon.isClosed()){
dbCon.close();
}
}catch(Exception errClose){
errClose.printStackTrace();
}
}
//===closeResulteSet方法,关闭结果集对象
public static void closeReselteSet(ResultSet rs){
try{
if(rs !=null ){
//rs.close();
rs =null;
}
}catch(Exception errClose){
errClose.printStackTrace();
}
}
//====closePreparedStatement方法,关闭命令对象
public static void closePreparedStatement(PreparedStatement pstat){
try{
if(pstat !=null){
pstat.close();
}
}catch(Exception errClose){
errClose.printStackTrace();
}
}
}
/**
* filenamr:News.java;
* 实现新闻管理系统的主页面
* author:STAR
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentsUse extends JFrame implements ActionListener{
//声明成员变量
private JPanel pan;
private MenuBar bar;
private Menu file,
help;
private MenuItem file_login,
file_adminlogin,
file_exit,
help_liaotian;
private JLabel lb,imge;
private ImageIcon img;
//构造方法,实现初始化
public StudentsUse(){
pan=new JPanel();
bar=new MenuBar();
file=new Menu("登录");
help=new Menu("帮助");
file_adminlogin=new MenuItem("管理员登录");
file_login=new MenuItem("登录");
file_exit=new MenuItem("退出");
help_liaotian=new MenuItem("咨询");
lb=new JLabel("BCIT班费管理系统");
lb.setFont(new Font("黑体",Font.BOLD,32));
img=new ImageIcon("333.jpg");
imge=new JLabel (img);
//注册监听
file_login.addActionListener(this);
file_exit.addActionListener(this);
help_liaotian.addActionListener(this);
//--------------实现界面-----------------
//在窗体上添加菜单栏
this.setMenuBar(bar);
//给菜单栏添加菜单
bar.add(file);
bar.add(help);
//给file菜单添加菜单项
file.add(file_login);
file.add(new MenuItem("-"));
file.add(file_exit);
help.add(help_liaotian);
pan.add(lb);
pan.add(imge);
//--------------------------------------
this.getContentPane().add(pan);
this.setSize(520,350);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//创建菜单监听类,实现菜单事件的处理
public void actionPerformed(ActionEvent evt){
if(evt.getSource().equals(file_login)){
Login star=new Login();
star.setVisible(true);
this.setVisible(false);
//this.dispose();也是销毁当前的这个窗口
}
if(evt.getSource().equals(file_exit)){
System.exit(0);
}
if(evt.getSource().equals(help_liaotian)){
MyClient star=new MyClient();
star.setVisible(true);
}
}
//main方法,程序入口
public static void main(String[]args){
new StudentsUse();
}
}
本回答被提问者采纳
已赞过
已踩过<
你对这个回答的评价是?
评论
收起