package com.zyp;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.sql.*;
class Login extends JFrame implements ActionListener
{ Container cp=null;
JFrame f=null;
JButton j1,j2;
JTextField t1;
JPasswordField t2;
JLabel jlable1,jlable2;
Color c;
JPanel jp1,jp2;
Login(){
f=new JFrame("图书管理系统");
j1=new JButton("确定");
j2=new JButton("取消");
cp=f.getContentPane();
jlable1=new JLabel(" 用户名");
jlable2=new JLabel(" 密码");
jp1=new JPanel();
jp2=new JPanel();
t1=new JTextField(18);
t2=new JPasswordField(18);
jp1.add(jlable1);
jp1.add(t1);
jp1.add(jlable2);
jp1.add(t2);
JLabel JL=new JLabel("欢迎登陆",SwingConstants.CENTER);
cp.add(JL,"North");
jp2.add(j1);
jp2.add(j2);
cp.add(jp1,"Center");
cp.add("South",jp2);
jp1.setBackground(new Color(255,153,255));
Toolkit kit=Toolkit.getDefaultToolkit(); //当需要解释类型、方法或构造方法的一般签名信息的反射方法遇到语法错误的签名属性时,抛出该错误。
Dimension screen=kit.getScreenSize();
int x=screen.width;
/*取得显示器窗口的宽度*/
int y=screen.height;
/*取得显示器窗口的高度*/
//setSize(x,y); /*让系统窗口平铺整个显示器窗口*/
f.setSize(300,300);
int xcenter=(x-300)/2;
int ycenter=(y-300)/2;
f.setLocation(xcenter,ycenter);/*显示在窗口中央*/
f.setVisible(true);
j1.addActionListener(this);//注册事件监听器
j2.addActionListener(this);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public void confirm()//验证用户和密码是否存在
{
Connection con = null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){System.out.println("加载驱动程序失败!");}
try{
String url = "jdbc:sqlserver://localhost:1433;databasename=model";//直接使用当前类目录下的数据库文件
String user="sa";
String password ="ss";
con =DriverManager.getConnection(url, user, password);
//JDBC中已经有多个驱动,此句话是遍历系统中已经注册的驱动程序,直到找到一个正确的驱动程序。
// Statement sql=con.createStatement();
String uname=t1.getText().trim();
String Mima=t2.getText().trim();
// String queryMima="select * from [book] where 用户名='"+uname+"' and 密码='"+Mima+"'";
String queryMima="select * from [book]";
PreparedStatement sql=con.prepareStatement(queryMima);
// ResultSet rs=sql.executeQuery(queryMima);
ResultSet rs=sql.executeQuery();
while(rs.next())
{
if(rs.getString(1).trim().equals(uname)&&rs.getString(2).trim().equals(Mima))
{
if(rs.getString(3).trim().equals("1"))
{
new Book(uname);
f.hide();
return;
}
else{
new book1(uname);
f.hide();
return;
}
}
}
con.close();
{
JOptionPane.showMessageDialog(null,"该用户不存在","提示!",
JOptionPane.YES_NO_OPTION);
}
t1.setText("");
t2.setText("");
} catch(SQLException g)
{
System.out.println("E Code "+g.getErrorCode());
System.out.println("E M"+g.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("确定")){
confirm();
}
else if(cmd.equals("取消")){
f.dispose();
}
}
public static void main(String []arg){