/*一个山寨版本的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");
}
}
分享到:
相关推荐
NULL 博文链接:https://jayxigua.iteye.com/blog/698839
2012-06-11 21:44 2,279 C语言编一个程序完成64位数据(无符号)的加法,减法运算.txt 2012-06-11 21:43 1,480,155 Direct3D加载3d文件.rar 2012-06-11 21:29 22,102 DSP编程一周通.rar 2012-06-11 21:04 837,926 ...
2012-06-12 12:54 183,362 写一个图形界面的操作系统.rar 2012-06-12 11:51 49,152 列主元消去法.doc 2012-06-12 12:20 524 判断质数.dsw 2012-06-12 12:12 1,987,273 单词拼写检查器.rar 2012-06-12 11:40 2,477,...
NULL 博文链接:https://jayxigua.iteye.com/blog/690203
NULL 博文链接:https://jayxigua.iteye.com/blog/686371
NULL 博文链接:https://jayxigua.iteye.com/blog/691025
学生读书笔记共享-学生读书笔记共享系统的设计与实现代码-java-springboot-基于springboot的学生读书笔记共享系统项目-代码-源码-项目-系统-毕设-网站 1、技术栈:java,springboot,vue,ajax,maven,mysql,...
Java笔记---李兴华---Java笔记---李兴华-----Java笔记---李兴华--Java笔记---李兴华
zookeeper云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-...
java各知识点详细总结(毕向东笔记整理)。第一章:编程基础 3-11 第二章:数组 11 -31 第三章:面向对象程序开发 31 -74 第四章:异常机制 74 -89 第五章:多线程技术 89 -122122122 第六章:常用类 API 122API 122 ...
NULL 博文链接:https://jayxigua.iteye.com/blog/698837
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
JAVA学习经典笔记-----1JAVA学习经典笔记-----1JAVA学习经典笔记-----1JAVA学习经典笔记-----1JAVA学习经典笔记-----1