文档库 最新最全的文档下载
当前位置:文档库 › java 实训课题

java 实训课题


//【习11.2】 用户注册与登录。
//数据库

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

public class RegisterEnterJFrame extends JFrame implements ActionListener, WindowListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField user; //用户名文本行
private JPasswordField password; //密码文本行
private Connection conn; //数据库连接对象
private String table;

//构造方法,driver指定JDBC驱动程序,url指定数据库名,table指定表名
public RegisterEnterJFrame(String driver, String url, String table)
throws ClassNotFoundException,SQLException
{
super("用户注册与登录");
this.setBounds(300,240,210,180);
this.setBackground(Color.lightGray);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout());

this.getContentPane().add(new JLabel("用户名"));
user = new JTextField(10);
user.setEditable(true);
this.getContentPane().add(user);
this.getContentPane().add(new JLabel("密 码"));
password = new JPasswordField(10);
this.getContentPane().add(password);






String bstr[]={"登录"};
JButton buttons[] = new JButton[bstr.length];



String bstr1[]={"查询"};
JButton buttons1[] = new JButton[bstr1.length];
String bstr2[]={"注册"};
JButton buttons2[] = new JButton[bstr1.length];
String bstr3[]={"插入"};
JButton buttons3[] = new JButton[bstr1.length];





for (int i=0; i{
buttons[i] = new JButton(bstr[i]);
this.getContentPane().add(buttons[i]);
buttons[i].addActionListener(this);//添加监听
}



for (int i=0; i{
buttons1[i] = new JButton(bstr1[i]);
this.getContentPane().add(buttons1[i]);
buttons1[i].addActionListener(this);//添加监听
}
for (int i=0; i{
buttons2[i] = new JButton(bstr2[i]);
this.getContentPane().add(buttons2[i]);
buttons2[i].addActionListener(this);//添加监听
}
for (int i=0; i{
buttons3[i] = new JButton(bstr3[i]);
this.getContentPane().add(buttons3[i]);
buttons3[i].addActionListener(this);//添加监听
}







this.setVisible(true);

this.addWindowListener(this);
Class.forName(driver); //指定JDBC驱动程序
this.conn=DriverManager.getConnection(url,"sa","123456"); //返回数据库连接对象
this.table=table; //table指定数据库中的表名
}

public void actionPerformed(ActionEvent e) //单击事件处理方法
{
String name = user.getText();
String password2 = new String(password.getText());
if (name.equals(""))
JOptionPane.showMessageDialog(this, "请输入用户名");
else if (password.equals(""))
JOptionPane.showMessageDialog(this, "密码不能为空,请输入密码");
else
try
{
if (e.getActionCommand().equals("注册")) //单击注册按钮
register(name,password2); //注册该用户
else if (e.getActionCommand().equals("登录"))//单击登录按钮
enter(name,password2); //用户登录
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
}

//注册指定用户。在用户表中查找name,若有同名者,则不能注册;否则在表中添加一行
private void register(String name, String password) throws SQLException
{
String sql="SELECT * FROM "+this.table;
Statement stmt=this.conn.createStatement(1003,1008);//创建语句对象,可更新
ResultSet rset=stmt.executeQuery(sql); //执行数据查询SELECT语句
boolean find=false; //表示两种查找结果的信号量
while (!find && rset.next()) //未找到时迭代遍历结果集
find = rset.getString(1).equals(name); //寻找相同用户名者
if (find) //找到
JOptionPane.showMessageDialog(this, name+"用户名已注册,请更改用户名。");
else //没找到,则注册,即在结果集中插入一行
{
rset.moveToInsertRow(); //当前行指针移动至待插入行
rset.updateString("name", name); //设置指定列的取值
rset.updateString("password", password);
rset.insertRow(); //插入一行提交数据库
JOptionPane.showMessageDialog(this, name+"用户名注册成功");
}
rset.close(); //关闭结果集
stmt.close(); //关闭语句
}

//登录,在用户表中查找指定用户,若用户存在且密码

匹配,则登录成功
private void enter(String name, String password) throws SQLException
{
String sql="SELECT * FROM "+this.table;
Statement stmt=this.conn.createStatement(); //创建语句对象
ResultSet rset=stmt.executeQuery(sql); //执行数据查询SELECT语句
boolean find=false; //表示两种查找结果的信号量
while (!find && rset.next()) //未找到时迭代遍历结果集
if (rset.getString(1).equals(name)) //用户名相同
{
if (rset.getString(2).equals(password)) //密码相同
JOptionPane.showMessageDialog(this, name+"登录成功");
else //密码不同
JOptionPane.showMessageDialog(this, name+"密码错误,请重新输入");
find=true;
}
if (!find) //未找到
JOptionPane.showMessageDialog(this, "没有"+name+"用户,请重新输入用户名");
rset.close();
stmt.close();
}

public void windowClosing(WindowEvent e) //单击窗口关闭按钮时触发并执行
{
try
{
this.conn.close(); //关闭数据库连接
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void windowOpened(WindowEvent e) {} //打开窗口
public void windowClosed(WindowEvent e) {} //关闭窗口后
public void windowIconified(WindowEvent e) {} //窗口最小化
public void windowDeiconified(WindowEvent e) {} //窗口恢复
public void windowActivated(WindowEvent e) {} //激活窗口
public void windowDeactivated(WindowEvent e) {} //变为不活动窗口
public static void main(String args[]) throws Exception
{
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //指定MySQL JDBC驱动程序
String url="jdbc:sqlserver://localhost:1433; DatabaseName=resu";
new RegisterEnterJFrame(driver, url, "register");
}







/*
public int dataUpdate(String sql) throws SQLException
{
Statement statement = conn.createStatement();
int result = statement.executeUpdate(sql);
statement.close();
return result;
}

public void select(String sql) throws SQLException
{
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery(sql); //执行数据查询SELECT语句
if (resultset!=null)
{
ResultSetMetaData rsmd = resultset.getMetaData(); //返回元数据对象
int columnCount = rsmd.getCol

umnCount(); //获得列数
for(int j=1;j<=columnCount;j++)
System.out.print(rsmd.getColumnLabel(j)+"\t"); //获得列名

System.out.println();
while(resultset.next()) //从前向后访问每行
{
for(int j=1;j<=columnCount;j++) //获得每列值
System.out.print(resultset.getString(j)+"\t"); //获得当前行指定列的值
System.out.println();
}
}

resultset.close();
statement.close();
}


public static void main(String[] srg) throws SQLException {
String sql=null;
DBUtil db=new DBUtil();
System.out.println(db.getDBAbout());
sql="CREATE TABLE Persons(Id_P int," +
"LastName varchar(255),FirstName varchar(255)," +
"Address varchar(255),City varchar(255))";
db.dataUpdate(sql);
sql="INSERT INTO Persons VALUES (1,'11','11','11','11')";
db.dataUpdate(sql);
sql="INSERT INTO Persons VALUES (2,'22','22','22','22')";
db.dataUpdate(sql);
sql="INSERT INTO Persons VALUES (3,'33','33','33','33')";
db.dataUpdate(sql);
sql="INSERT INTO Persons VALUES (4,'44','44','44','44')";
db.dataUpdate(sql);
sql="DELETE FROM Persons WHERE Id_P = 1";
db.dataUpdate(sql);
sql="UPDATE Persons SET FirstName = '55' WHERE Id_P = 2";
db.dataUpdate(sql);
sql="select * from Student";
db.select(sql);*/






}
}

/*
1、register()和enter()方法中,不用find表示查找结果,而写成如下,不适用于空表。
while (rset.next() && !rset.getString(1).equals(name)) //用户名不相同时继续迭代遍历结果集
{}
if (!rset.isAfterLast()) //非空表时找到

2、 不执行
public void finalize() throws SQLException //析构方法,关闭数据库连接
{
this.conn.close();
}
*/


相关文档