Enter password: ***
Welcome to the MySQL monitor. Commands end with ; or \g.
create database class_db ;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| class_db |
| da_database10 |
| mysql |
| test |
+--------------------+
mysql> use class_db;
Database changed
mysql> create table admin ( id int(8) not null primary key auto_increment,
-> name varchar(30) ,
-> password varchar(30) );
Query OK, 0 rows affected (0.64 sec)
mysql> show tables;
+--------------------+
| Tables_in_class_db |
+--------------------+
| admin |
+--------------------+
1 row in set (0.06 sec)
mysql> create table student( id varchar(50) not null primary key ,
-> name varchar(10) not null ,
-> password varchar(50) ,
-> jiguan varchar(10) not null ,
-> department varchar(10) ,
-> sex varchar(10) ,
-> mark int(11) ,
-> tel varchar(50) ,
-> e_mail varchar(50) );
Query OK, 0 rows affected (0.23 sec)
mysql> create table classes ( id varchar(50) not null ,
-> tea_id varchar(10) not null primary key ,
-> cour_id varchar(10) not null ,
-> room_id varchar(50) not null ,
-> cour_time varchar(10) not null );
Query OK, 0 rows affected (0.25 sec)
mysql> create table course ( id varchar(10) not null primary key ,
-> name varchar(20) not null ,
-> mark int(11) not null ,
-> prepare varchar(10) ,
-> dep varchar(10) );
Query OK, 0 rows affected (0.20 sec)
mysql> create table enrol ( stu_id varchar(50) not null primary key ,
-> class_id varchar(50) not null ,
-> accept tinyint(1) ,
-> score varchar(50) );
Query OK, 0 rows affected (0.25 sec)
mysql> create table teacher ( id varchar(10) not null ,
-> name varchar(50) not null ,
-> title varchar(50) not null ,
-> password varchar(50) not null );
Query OK, 0 rows affected (0.25 sec)
各表插入数据:
mysql> insert into admin values(1 , 'limq',1 ) ;
Query OK, 1 row affected (0.48 sec)
mysql> insert into student values(1 , '韩博', 1, '山东' , '机械系', '女', 9, 3317138,'[email protected]' ) ;
Enter password: ***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.68-community MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use class_db;
Database changed
mysql> show tables;
+--------------------+
| Tables_in_class_db |
+--------------------+
| admin |
| classes |
| course |
| enrol |
| student |
| teacher |
+--------------------+
6 rows in set (0.05 sec)
mysql> insert into student values(1 , '韩博', 1, '山东' , '机械系', '女', 9,
3317138, [email protected] ) ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '@qq.c
om )' at line 1
mysql> insert into student values(1 , '韩博', 1, '山东' , '机械系', '女', 9,
3317138, '[email protected]' ) ;
Query OK, 1 row affected (0.33 sec)
mysql> select * from student;
+----+------+----------+--------+------------+------+------+---------+----------
--+
| id | name | password | jiguan | department | sex | mark | tel | e_mail
|
+----+------+----------+--------+------------+------+------+---------+----------
--+
| 1 | 韩博 | 1 | 山东 | 机械系 | 女 | 9 | 3317138 | [email protected]
m |
+----+------+----------+--------+------------+------+------+---------+----------
--+
1 row in set (0.09 sec)
mysql> insert into student values( 2 , '王强', 1, '陕西' , '机械系', '男', 5,
3317138, '[email protected]' ) ;
Query OK, 1 row affected (0.13 sec)
mysql> select * from student;
+----+------+----------+--------+------------+------+------+---------+----------
--+
| id | name | password | jiguan | department | sex | mark | tel | e_mail
|
+----+------+----------+--------+------------+------+------+---------+----------
--+
| 1 | 韩博 | 1 | 山东 | 机械系 | 女 | 9 | 3317138 | [email protected]
m |
| 2 | 王强 | 1 | 陕西 | 机械系 | 男 | 5 | 3317138 | [email protected]
m |
+----+------+----------+--------+------------+------+------+---------+----------
--+
2 rows in set (0.00 sec)
mysql> insert into student values( 3 , '张永', 1, '河南' , '电子系', '男', 0,
113317138, '[email protected]' ) ;
Query OK, 1 row affected (0.14 sec)
mysql> select * from student;
+----+------+----------+--------+------------+------+------+-----------+--------
----+
| id | name | password | jiguan | department | sex | mark | tel | e_mail
|
+----+------+----------+--------+------------+------+------+-----------+--------
----+
| 1 | 韩博 | 1 | 山东 | 机械系 | 女 | 9 | 3317138 | 123@qq.
com |
| 2 | 王强 | 1 | 陕西 | 机械系 | 男 | 5 | 3317138 | 124@qq.
com |
| 3 | 张永 | 1 | 河南 | 电子系 | 男 | 0 | 113317138 | 125@qq.
com |
+----+------+----------+--------+------------+------+------+-----------+--------
----+
3 rows in set (0.00 sec)
mysql> insert into classes values( 1 , 2, 1, 101 , Mon_1 ) ;
ERROR 1054 (42S22): Unknown column 'Mon_1' in 'field list'
mysql> show columns from classes;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | varchar(50) | NO | | NULL | |
| tea_id | varchar(10) | NO | PRI | NULL | |
| cour_id | varchar(10) | NO | | NULL | |
| room_id | varchar(50) | NO | | NULL | |
| cour_time | varchar(10) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.16 sec)
mysql> insert into classes values( 1 , 2, 1, 101 , 'Mon_1' ) ;
Query OK, 1 row affected (0.17 sec)
mysql> insert into classes values( 2 , 3, 3, 411 , 'Mon_3' ) ;
Query OK, 1 row affected (0.11 sec)
mysql> insert into classes values( 4 , 3, 4, 555 , 'Mon_4' ) ;
ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'
mysql> insert into classes values( 3 , 3, 4, 555 , 'Mon_4' ) ;
ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'
mysql> insert into classes values( 3 , 4, 4, 555 , 'Mon_4' ) ;
Query OK, 1 row affected (0.23 sec)
mysql> insert into classes values( 8 , 5, 7, 747 , 'Tues_3' ) ;
Query OK, 1 row affected (0.14 sec)
mysql> insert into classes values( 12 , 6, 11, 203 , 'Tues_2' ) ;
Query OK, 1 row affected (0.14 sec)
mysql> select * from classes;
+----+--------+---------+---------+-----------+
| id | tea_id | cour_id | room_id | cour_time |
+----+--------+---------+---------+-----------+
| 1 | 2 | 1 | 101 | Mon_1 |
| 2 | 3 | 3 | 411 | Mon_3 |
| 3 | 4 | 4 | 555 | Mon_4 |
| 8 | 5 | 7 | 747 | Tues_3 |
| 12 | 6 | 11 | 203 | Tues_2 |
+----+--------+---------+---------+-----------+
5 rows in set (0.00 sec)
mysql> show columns from course;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | varchar(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| mark | int(11) | NO | | NULL | |
| prepare | varchar(10) | YES | | NULL | |
| dep | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.11 sec)
mysql> insert into classes values( 1 , 'c语言', 3, 8, '计算机' ) ;
Query OK, 1 row affected (0.20 sec)
mysql> insert into classes values( 10 , '模拟线路', 5, 9, '电子系' ) ;
Query OK, 1 row affected (0.11 sec)
mysql> insert into classes values( 11 , '邓小平理论', 4, 0, public ) ;
ERROR 1054 (42S22): Unknown column 'public' in 'field list'
mysql> insert into classes values( 11 , '邓小平理论', 4, 0, 'public' ) ;
Query OK, 1 row affected (0.14 sec)
mysql> insert into classes values( 2 , '英语', 1, 0, 'public' ) ;
Query OK, 1 row affected (0.09 sec)
mysql> insert into classes values( 3 , '计算机组成原理', 5, 8, '计算机' ) ;
Query OK, 1 row affected (0.25 sec)
mysql> insert into classes values( 4 , '机械制造', 5, 8, '机械系' ) ;
Query OK, 1 row affected (0.11 sec)
mysql> insert into classes values( 5 , '计算机英语', 5, 2, '计算机' ) ;
Query OK, 1 row affected (0.16 sec)
mysql> insert into classes values( 6 , '体育', 4, 0, 'public' ) ;
Query OK, 1 row affected (0.17 sec)
mysql> insert into classes values( 7 , '美术', 4, 0, 'public' ) ;
Query OK, 1 row affected (0.11 sec)
mysql> insert into classes values( 8 , '高数', 5, 0, 'public' ) ;
Query OK, 1 row affected (0.08 sec)
mysql> insert into classes values( 9 , '电路分析', 4, 8, '电子系' ) ;
Query OK, 1 row affected (0.14 sec)
mysql> select * from classes;
+----+----------------+---------+---------+-----------+
| id | tea_id | cour_id | room_id | cour_time |
+----+----------------+---------+---------+-----------+
| 1 | 2 | 1 | 101 | Mon_1 |
| 2 | 3 | 3 | 411 | Mon_3 |
| 3 | 4 | 4 | 555 | Mon_4 |
| 8 | 5 | 7 | 747 | Tues_3 |
| 12 | 6 | 11 | 203 | Tues_2 |
| 1 | c语言 | 3 | 8 | 计算机 |
| 11 | 邓小平理论 | 4 | 0 | public |
| 9 | 电路分析 | 4 | 8 | 电子系 |
| 8 | 高数 | 5 | 0 | public |
| 4 | 机械制造 | 5 | 8 | 机械系 |
| 5 | 计算机英语 | 5 | 2 | 计算机 |
| 3 | 计算机组成原理 | 5 | 8 | 计算机 |
| 7 | 美术 | 4 | 0 | public |
| 10 | 模拟线路 | 5 | 9 | 电子系 |
| 6 | 体育 | 4 | 0 | public |
| 2 | 英语 | 1 | 0 | public |
+----+----------------+---------+---------+-----------+
16 rows in set (0.00 sec)
mysql>
Enter password: ***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.68-community MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use classes_db;
ERROR 1049 (42000): Unknown database 'classes_db'
mysql> use class_db;
Database changed
mysql> show tables;
+--------------------+
| Tables_in_class_db |
+--------------------+
| admin |
| classes |
| course |
| enrol |
| student |
| teacher |
+--------------------+
6 rows in set (0.00 sec)
mysql> show columns from classes;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | varchar(50) | NO | | NULL | |
| tea_id | varchar(10) | NO | PRI | NULL | |
| cour_id | varchar(10) | NO | | NULL | |
| room_id | varchar(50) | NO | | NULL | |
| cour_time | varchar(10) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> select * from classes;
+----+----------------+---------+---------+-----------+
| id | tea_id | cour_id | room_id | cour_time |
+----+----------------+---------+---------+-----------+
| 1 | 2 | 1 | 101 | Mon_1 |
| 2 | 3 | 3 | 411 | Mon_3 |
| 3 | 4 | 4 | 555 | Mon_4 |
| 8 | 5 | 7 | 747 | Tues_3 |
| 12 | 6 | 11 | 203 | Tues_2 |
| 1 | c语言 | 3 | 8 | 计算机 |
| 11 | 邓小平理论 | 4 | 0 | public |
| 9 | 电路分析 | 4 | 8 | 电子系 |
| 8 | 高数 | 5 | 0 | public |
| 4 | 机械制造 | 5 | 8 | 机械系 |
| 5 | 计算机英语 | 5 | 2 | 计算机 |
| 3 | 计算机组成原理 | 5 | 8 | 计算机 |
| 7 | 美术 | 4 | 0 | public |
| 10 | 模拟线路 | 5 | 9 | 电子系 |
| 6 | 体育 | 4 | 0 | public |
| 2 | 英语 | 1 | 0 | public |
+----+----------------+---------+---------+-----------+
16 rows in set (0.00 sec)
mysql> select * from course;
Empty set (0.00 sec)
mysql> insert into course values( 1 , 'c语言', 3, 8, '计算机' ) ;
Query OK, 1 row affected (0.11 sec)
mysql> insert into course values( 10 , '模拟线路', 5, 9, '电子系' ) ;
Query OK, 1 row affected (0.12 sec)
mysql> insert into course values( 11 , '邓小平理论', 4, 0, 'public' ) ;
Query OK, 1 row affected (0.09 sec)
mysql> insert into course values( 2 , '英语', 1, 0, 'public' ) ;
Query OK, 1 row affected (0.11 sec)
mysql> insert into course values( 3 , '计算机组成原理', 5, 8, '计算机' ) ;
Query OK, 1 row affected (0.09 sec)
mysql> insert into course values( 4 , '机械制造', 5, 8, '机械系' ) ;
Query OK, 1 row affected (0.09 sec)
mysql> insert into course values( 5 , '计算机英语', 5, 2, '计算机' ) ;
Query OK, 1 row affected (0.11 sec)
mysql> insert into course values( 6 , '体育', 4, 0, 'public' ) ;
Query OK, 1 row affected (0.14 sec)
mysql> insert into course values( 7 , '美术', 4, 0, 'public' ) ;
Query OK, 1 row affected (0.19 sec)
mysql> insert into course values( 8 , '高数', 5, 0, 'public' ) ;
Query OK, 1 row affected (0.14 sec)
mysql> insert into course values( 9 , '电路分析', 4, 8, '电子系' ) ;
Query OK, 1 row affected (0.16 sec)
mysql> select * from course;
+----+----------------+------+---------+--------+
| id | name | mark | prepare | dep |
+----+----------------+------+---------+--------+
| 1 | c语言 | 3 | 8 | 计算机 |
| 10 | 模拟线路 | 5 | 9 | 电子系 |
| 11 | 邓小平理论 | 4 | 0 | public |
| 2 | 英语 | 1 | 0 | public |
| 3 | 计算机组成原理 | 5 | 8 | 计算机 |
| 4 | 机械制造 | 5 | 8 | 机械系 |
| 5 | 计算机英语 | 5 | 2 | 计算机 |
| 6 | 体育 | 4 | 0 | public |
| 7 | 美术 | 4 | 0 | public |
| 8 | 高数 | 5 | 0 | public |
| 9 | 电路分析 | 4 | 8 | 电子系 |
+----+----------------+------+---------+--------+
11 rows in set (0.00 sec)
mysql> insert into enrol values( 1 , 7, 0 , 0 ) ;
Query OK, 1 row affected (0.22 sec)
mysql> insert into enrol values( 1 , 8, 0 , 0 ) ;
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into enrol values( 2 , 8, 0 , 0 ) ;
Query OK, 1 row affected (0.13 sec)
mysql> insert into teacher values( 1 , '李宁' , '教授',1 ) ;
Query OK, 1 row affected (0.16 sec)
mysql> insert into teacher values( 2 , '李强' , '讲师',1 ) ;
Query OK, 1 row affected (0.16 sec)
mysql> insert into teacher values( 3 , '网吧' , '讲师',1 ) ;
Query OK, 1 row affected (0.11 sec)
mysql> insert into teacher values( 4 , '马刚' , '讲师',1 ) ;
Query OK, 1 row affected (0.13 sec)
mysql>
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 59 to server version: 5.0.22-community-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use db_1;
Database changed
mysql> show tables;
+----------------+
| Tables_in_db_1 |
+----------------+
| admin |
| student |
| tb_books |
+----------------+
3 rows in set (0.08 sec)
mysql> create table classes ( id varchar(50) not null ,
-> tea_id varchar(10) not null primary key,
-> cour_id varchar(10) not null ,
-> room_id varchar(50) not null ,
-> cour_time varchar(10) not null );
Query OK, 0 rows affected (0.49 sec)
mysql> insert into classes values( 2 ,2 ,1, 101, 'mon_1');
Query OK, 1 row affected (0.17 sec)
mysql> insert into classes values( 3 ,1 ,3, 411, 'mon_3');
Query OK, 1 row affected (0.11 sec)
mysql> insert into classes values( 4 ,3 ,4, 555, 'mon_4');
Query OK, 1 row affected (0.12 sec)
mysql> insert into classes values( 5 ,6 ,5, 121, 'tues_4');
Query OK, 1 row affected (0.33 sec)
mysql> insert into classes values( 6 ,7 ,6, 454, 'tues_2');
Query OK, 1 row affected (0.25 sec)
mysql> show tables;
+----------------+
| Tables_in_db_1 |
+----------------+
| admin |
| classes |
| student |
| tb_books |
+----------------+
4 rows in set (0.00 sec)
mysql> create table course ( id varchar(10) not null primary key ,
-> name varchar(20) not null ,
-> mark int(11) not null ,
-> prepare varchar(10) ,
-> dep varchar(10) );
Query OK, 0 rows affected (0.26 sec)
mysql> insert into course values( 1 ,'c语言' ,3, 8, '计算机');
Query OK, 1 row affected (0.20 sec)
mysql> insert into course values( 10 ,'模拟电路' ,5, 9, '电子系');
Query OK, 1 row affected (0.16 sec)
mysql> insert into course values( 11 ,'邓小平理论' ,4 , 0 , 'public');
Query OK, 1 row affected (0.13 sec)
mysql> insert into course values( 2 ,'英语' ,1 , 0 , 'public');
Query OK, 1 row affected (0.17 sec)
mysql> insert into course values( 3 ,'计算机组成原理' ,5 , 8 , '机械系');
Query OK, 1 row affected (0.15 sec)
mysql> insert into course values( 4 ,'机械制造' ,5 , 8 , '机械系');
Query OK, 1 row affected (0.11 sec)
mysql> insert into course values( 5 ,'计算机应用' ,5 , 2 , '计算机');
Query OK, 1 row affected (0.19 sec)
mysql> insert into course values( 6 ,'体育' ,4 , 0 , 'public');
Query OK, 1 row affected (0.18 sec)
mysql> create table enrol ( stu_id varchar(50) not null primary key ,
-> class_id varchar(50) not null ,
-> accept tinyint(1) ,
-> score varchar(50) );
Query OK, 0 rows affected (0.32 sec)
mysql> insert into enrol values( 1 ,7 ,0 ,0);
Query OK, 1 row affected (0.24 sec)
mysql> insert into enrol values( 1 ,8 ,0 ,0);
ERROR 1062 (23000): Duplicate entry '1' for key 1
mysql> insert into enrol values( 2 ,8 ,0 ,0);
Query OK, 1 row affected (0.13 sec)
mysql> show tables;
+----------------+
| Tables_in_db_1 |
+----------------+
| admin |
| classes |
| course |
| enrol |
| student |
| tb_books |
+----------------+
6 rows in set (0.01 sec)
mysql> create table teacher ( id varchar(10) not null ,
-> name varchar(50) not null ,
-> title varchar(50) not null ,
-> password varchar(50) not null );
Query OK, 0 rows affected (0.28 sec)
mysql> insert into teacher values( 1 ,'马刚' ,'教授' ,1);
Query OK, 1 row affected (0.20 sec)
mysql> insert into teacher values( 2 ,'沈艳静' ,'教授' ,1);
Query OK, 1 row affected (0.08 sec)
mysql> insert into teacher values( 3 ,'春艳' ,'教授' ,1);
Query OK, 1 row affected (0.22 sec)
mysql> insert into teacher values( 4 ,'栗元邦' ,'博导' ,1);
Query OK, 1 row affected (0.24 sec)
mysql> show tables;
+----------------+
| Tables_in_db_1 |
+----------------+
| admin |
| classes |
| course |
| enrol |
| student |
| tb_books |
| teacher |
+----------------+
7 rows in set (0.00 sec)
mysql>
----------------------------------------------------------------------------
创建数据 连接操作 类文件
import java.io.*;
import java.sql.*;
public class sqlBean {
public Connection conn=null; //数据库连接
public ResultSet rs=null; //记录集
//重载数据库驱动
private String DatabaseDriver="org.gjt.mm.mysql.Driver";
//DataSource 数据源名称 DSN
private String DatabaseConnStr="jdbc:mysql:/localhost/class_DB?user=root&password=&useUnicode=true&characterEncoding=8859_1" ;
/*setXxx 用于设置属性值;getXxx 用于得到属性值*/
public void setDatabaseDriver(String Driver){
this.DatabaseDriver=Driver;
}
public String getDatabaseDriver(){
return (this.DatabaseDriver);
}
public void setDatabaseConnStr(String ConnStr){
this.DatabaseConnStr=ConnStr;
}
public String getDatabaseConnStr(){
return (this.DatabaseConnStr);
}
public sqlBean(){/////构造函数
try{
Class.forName(DatabaseDriver).newInstance(); //注册数据库驱动程序
}
catch(Exception e){ //输出结果,方便调试
System.err.println("加载驱动器有错误:"+e.getMessage( ));
System.out.print("执行插入有错误:"+e.getMessage());//输出到客户端
}
}
//执行插入数据库操作
public int executeInsert(String sql){
int num=0;
try{
//创建数据库连接
conn = DriverManager.getConnection(DatabaseConnStr);
//创建 JDBC 声明
Statement stmt=conn.createStatement( );
//执行指令
num=stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println("执行插入有错误:"+ex.getMessage() );
System.out.print("执行插入有错误:"+ex.getMessage());//输出到客户端
}
//关闭连接
CloseDataBase();
//返回结果
return num;
}
// 入口参数为 sql 语句,返回 ResultSet 对象
public ResultSet executeQuery(String sql){
rs=null;
try{
//创建数据库连接
conn = DriverManager.getConnection(DatabaseConnStr);
//创建 JDBC 声明
Statement stmt=conn.createStatement( );
//执行查询命令
rs=stmt.executeQuery(sql);
}
catch(SQLException ex){
System.err.println("执行查询有错误:"+ex.getMessage() );
System.out.print("执行查询有错误:"+ex.getMessage()); //输出到客户端
}
//获得查询结果
return rs;
}
//用增加,删除数据记录的操作
public int executeDelete(String sql){
int num=0;
try{
//创建数据库连接
conn = DriverManager.getConnection(DatabaseConnStr);
//创建一个 JDBC 声明
Statement stmt=conn.createStatement( );
//执行指令
num=stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.err.println("执行删除有错误:"+ex.getMessage() );
System.out.print("执行删除有错误:"+ex.getMessage()); //输出到客户端
}
//关闭连接
CloseDataBase();
//返回结果
return num;
}
//关闭数据库连接
public void CloseDataBase(){
try{
//关闭连接
conn.close();
}
catch(Exception end){
System.err.println("执行关闭 Connection 对象有错误:"+end.getMessage( ) );
System.out.print("执行执行关闭 Connection 对象有错误:有错误:"+end.getMessage()); //输出到客户端
}
}
}
---------------------------------------------------------------------------------------------------------------------------
系统登录界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="java.sql.*" errorPage="errorpage.jsp"
%>
登陆
Insert title here
<%
String getmessage = (String) session.getAttribute("error");
if (getmessage==null) {getmessage="";}
%>
<%=getmessage%>
学生课
绩管理系统
------------------------------------------------------------------------------
login_confirm .java shi servlet
package cla;
import java.io.*;
import java.sql.*;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class login_confirm
*/
@WebServlet("/login_confirm")
public class login_confirm extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse
res)
throws ServletException, IOException {
String message = null;
String id = null;
id = req.getParameter("id");
HttpSession session = req.getSession(true);
//把用户ID保存到Session中,这样可以在别的页面中得到当前登录信息
session.setAttribute("id", String.valueOf(id));
String password = null;
//获得从页面中提交的信息
password = req.getParameter("password");
String kind = null;
kind = req.getParameter("kind");
//根据当前用户的ID,来得到查询数据库得到密码
String temp = getPassword(req, res, id, kind);
if (password.equals(temp))
//当用户的认证通过后,通过此函数来跳转到不同的显示页面
goo(req, res, kind);
else {
message = "用户名或密码有误!";
//当出错时,把出错信息显示给用户,并跳转到出错页面中去
doError(req, res, message);
}
}
/**
* 当用户的认证通过后,通过此函数来跳转到不同的显示页面
*/
public void goo(HttpServletRequest req, HttpServletResponse res,
String kind)
throws ServletException, IOException {
if (kind.equals("student")) {//如果是学生登录
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/student.jsp");
rd.forward(req, res);
}
if (kind.equals("teacher")) {//如果是教师登录
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/teacher.jsp");
rd.forward(req, res);
}
if (kind.equals("admin")) {//如果是管理员登录
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/admin.jsp");
rd.forward(req, res);
}
}
/**
* 根据当前用户的ID,来得到查询数据库得到密码
*/
public String getPassword(HttpServletRequest req,
HttpServletResponse res,String id, String kind) throws ServletException,
IOException {
sqlBean db = new sqlBean();
String pw = "";
String sql = "select password from " + kind + " where id='" + id +
"'";
try {
ResultSet rs = db.executeQuery(sql);
if (rs.next()) {
pw = rs.getString("password");
}
} catch (Exception e) {
System.out.print(e.toString());
}
return pw;
}
/**
* 当出错时,把出错信息显示给用户,并跳转到出错页面中去
*/
public void doError(HttpServletRequest req, HttpServletResponse res,
String str) throws ServletException, IOException {
req.setAttribute("problem", str);
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/errorpage.jsp");
rd.forward(req, res);
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String action = action = req.getParameter("action");
if ("logout".equalsIgnoreCase(action)) {
HttpSession session = req.getSession(true);
session.invalidate();
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/login.jsp");
rd.forward(req, res);
}
}
}