/*一个山寨版本的QQ,然后凭着我们上课老师讲的一个实例,和自己的修改写的。
* 1422942883这是我的QQ,欢迎各位学习编程的同学加我好友,
* 或者给我的个人主页留言(http:jayxigua.iteye.com)
* ,一起讨论,学习。呵呵。
*
主要的知识点是:一,GUI,操作,完成一个类似于QQ登录界面的小窗口。
TextField Choice Checkbox Button Panel bottom事件 Dialog diag
二,就是JDBC 的小运用:输入姓名和密码,进入数据库进行判断?
然后返回不同的对话框。
(我数据库里面的一个学生是:“kb”“24”)
数据库的sql语句,导入到mysql就OK了!
create database ascent_jdbc;
use ascent_jdbc;
create table student
(sid varchar(10) not null,
sname varchar(50),
spassword varchar(20),
primary key(sid)
);
insert into student values('1','kb','24');
insert into student values('2','kg','5');
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
public class Ascent20100621_dbdc_myqq extends Frame {
private Label name, pawd, regist, getPawd, st;
private TextField username, password;
private Choice status;
private Checkbox rem, auto;
private Button setup, login;
private Panel top, center, bottom;
private Dialog diag;
public Ascent20100621_dbdc_myqq(String title) {
super(title);
this.name = new Label("账号:", Label.CENTER);
this.pawd = new Label("密码:", Label.CENTER);
this.regist = new Label("注册新账号", Label.LEFT);
this.getPawd = new Label("取回密码", Label.LEFT);
this.st = new Label("状态:");
this.username = new TextField("<请输入账号>", 40);
this.password = new TextField(40);
this.password.setEchoChar('*');
this.top = new Panel(new GridLayout(2, 3, 5, 10));
this.top.add(this.name);
this.top.add(this.username);
this.top.add(this.regist);
this.top.add(this.pawd);
this.top.add(this.password);
this.top.add(this.getPawd);
this.st = new Label("状态:");
this.status = new Choice();
this.status.add("我在线上");
this.status.add("忙碌");
this.status.add("离开");
this.rem = new Checkbox("记住密码", false);
this.auto = new Checkbox("自动登陆", false);
this.center = new Panel();
this.center.add(this.st);
this.center.add(this.status);
this.center.add(this.rem);
this.center.add(this.auto);
this.bottom = new Panel(new FlowLayout(FlowLayout.CENTER, 100, 5));
this.setup = new Button("设置");
this.login = new Button("登陆");
MyListener ml = new MyListener();
this.login.addActionListener(ml);
this.setup.addActionListener(ml);
this.bottom.add(this.setup);
this.bottom.add(this.login);
this.add(this.top, BorderLayout.NORTH);
this.add(this.center);
this.add(this.bottom, BorderLayout.SOUTH);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
});
this.setBounds(200, 200, 350, 180);
this.setResizable(false);
this.setVisible(true);
}
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == login) {
String name = username.getText();
String pass = password.getText();
if (isValidUser(name, pass)) {
diag = new Dialog(Ascent20100621_dbdc_myqq.this, "正确",
false);
} else {
diag = new Dialog(Ascent20100621_dbdc_myqq.this, "错误",
false);
}
diag.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
diag.setVisible(false);
diag.dispose();
}
});
diag.setBounds(300, 300, 200, 150);
diag.add(new Label("真的要退出吗?", Label.CENTER));
diag.setVisible(true);
} else if (e.getSource() == setup) {
diag = new Dialog(Ascent20100621_dbdc_myqq.this, "设置页面", false);
diag.setBounds(300, 300, 200, 500);
diag.add(new Label("进行设置", Label.CENTER));
diag.setVisible(true);
}
}
}
public Connection getConnection() {
String url = "jdbc:mysql://localhost:3306/ascent_jdbc?useUnicode=true&characterEncoding=gbk";
String user = "root";
String DbPassword = "root";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("mysql未驱动");
e.printStackTrace();
}
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, DbPassword);
} catch (SQLException e) {
System.out.println("SQL 异常");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("数据库没有找到");
e.printStackTrace();
}
return conn;
}
// 判断密码是否正确!
boolean isValidUser(String name, String pass) {
Connection conn = this.getConnection();
Statement comm = null;
ResultSet rs;
boolean flag = false;
try {
String sql2 = "select * from student where sname = '" + name
+ "' and spassword = '" + pass + "'";
comm = conn.createStatement();
rs = comm.executeQuery(sql2);
if (rs.next() == false) {
System.out.print("用户名,密码错误!");
flag = false;
} else {
System.out.print("用户名,密码正确!");
flag = true;
}
} catch (SQLException e) {
System.out.println("异常!");
}
System.out.print(flag);
return flag;
}
void getSystemDBMD() {
Connection conn = this.getConnection();
try {
DatabaseMetaData dmd = conn.getMetaData();
System.out.println(dmd.getDatabaseProductName());
System.out.println(dmd.getDatabaseProductVersion());
System.out.println(dmd.getDatabaseMajorVersion());
System.out.println(dmd.getDatabaseMinorVersion());
System.out.println(dmd.getJDBCMajorVersion());
System.out.println(dmd.getJDBCMinorVersion());
} catch (SQLException e) {
e.printStackTrace();
}
}
void getfieldDBMD() {
Connection conn = this.getConnection();
Statement comm = null;
try {
comm = conn.createStatement();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = "select* from student";
ResultSet rs;
try {
rs = comm.executeQuery(sql);
DatabaseMetaData dmd = conn.getMetaData();
// System.out.println(dmd.getJDBCMinorVersion());
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Ascent20100621_dbdc_myqq myqq = new Ascent20100621_dbdc_myqq("QQ山寨版");
myqq.getSystemDBMD();
// 测试,一个正确,一个错误!
myqq.isValidUser("kb", "24");
myqq.isValidUser("kb", "224");
}
}