Java项目 1 - 学生成绩管理系统---采用 Servlet+Jsp+JavaBean+MySql 设计方式,

 

 

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,'123@qq.com' ) ;

 

 

 


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, 123@qq.com ) ;
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, '123@qq.com' ) ;
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 | 123@qq.co
m |
+----+------+----------+--------+------------+------+------+---------+----------
--+
1 row in set (0.09 sec)

mysql> insert  into student values( 2 , '王强', 1, '陕西' ,  '机械系', '男',  5,
  3317138, '124@qq.com' ) ;
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 | 123@qq.co
m |
| 2  | 王强 | 1        | 陕西   | 机械系     | 男   |    5 | 3317138 | 124@qq.co
m |
+----+------+----------+--------+------------+------+------+---------+----------
--+
2 rows in set (0.00 sec)

mysql> insert  into student values( 3 , '张永', 1, '河南' ,  '电子系', '男',  0,
  113317138, '125@qq.com' ) ;
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"
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<STYLE>A.menuitem {
COLOR: menutext; TEXT-DECORATION: none
}
A.menuitem:hover {
COLOR: highlighttext; BACKGROUND-COLOR: highlight
}
DIV.contextmenu {
BORDER-RIGHT: 2px outset; BORDER-TOP: 2px outset; Z-INDEX: 999;
VISIBILITY: hidden; BORDER-LEFT: 2px outset; BORDER-BOTTOM: 2px outset;
POSITION: absolute; BACKGROUND-COLOR: buttonface
}
</STYLE>
<SCRIPT language=JavaScript>
function Year_Month(){
var now = new Date();
var yy = now.getYear();
var mm = now.getMonth()+1;
var cl = '<font color="#0000df">';
if (now.getDay() == 0) cl = '<font color="#c00000">';
if (now.getDay() == 6) cl = '<font color="#00c000">';
return(cl + yy + '年' + mm + '月</font>'); }
function Date_of_Today(){
var now = new Date();
var cl = '<font color="#ff0000">';
if (now.getDay() == 0) cl = '<font color="#c00000">';
if (now.getDay() == 6) cl = '<font color="#00c000">';
return(cl + now.getDate() + '</font>'); }
function Day_of_Today(){
var day = new Array();
day[0] = "星期日";
day[1] = "星期一";
day[2] = "星期二";
day[3] = "星期三";
day[4] = "星期四";
day[5] = "星期五";
day[6] = "星期六";
var now = new Date();
var cl = '<font color="#0000df">';
if (now.getDay() == 0) cl = '<font color="#c00000">';
if (now.getDay() == 6) cl = '<font color="#00c000">';
return(cl + day[now.getDay()] + '</font>'); }
function CurentTime(){
var now = new Date();
var hh = now.getHours();
var mm = now.getMinutes();
var ss = now.getTime() % 60000;
ss = (ss - (ss % 1000)) / 1000;
var clock = hh+':';
if (mm < 10) clock += '0';
clock += mm+':';
if (ss < 10) clock += '0';
clock += ss;
return(clock); }
function refreshCalendarClock(){
document.all.calendarClock1.innerHTML = Year_Month();
document.all.calendarClock2.innerHTML = Date_of_Today();
document.all.calendarClock3.innerHTML = Day_of_Today();
document.all.calendarClock4.innerHTML = CurentTime(); }
var webUrl = webUrl;
document.write('<table border="0" cellpadding="0"
cellspacing="0"><tr><td>');
document.write('<table id="CalendarClockFreeCode" border="0"
cellpadding="0" cellspacing="0" width="60" height="70" ');
document.write('style="position:absolute;visibility:hidden"
bgcolor="#eeeeee">');
document.write('<tr><td align="center"><font ');
document.write('style="cursor:hand;color:#ff0000;font-family:宋
体;font-size:14pt;line-height:120%" ');
if (webUrl != 'netflower'){
document.write('</td></tr><tr><td align="center"><font ');
document.write('style="cursor:hand;color:#2000ff;font-family:宋
体;font-size:9pt;line-height:110%" ');
}
document.write('</td></tr></table>');
document.write('<table border="0" cellpadding="0" cellspacing="0"
width="61" bgcolor="#C0C0C0" height="70">');
document.write('<tr><td valign="top" width="100%" height="100%">');
document.write('<table border="1" cellpadding="0" cellspacing="0"
width="58" bgcolor="#FEFEEF" height="67">');
document.write('<tr><td align="center" width="100%" height="100%" >');
document.write('<font id="calendarClock1" style="font-family:宋
体;font-size:7pt;line-height:120%"> </font><br>');
document.write('<font id="calendarClock2"
style="color:#ff0000;font-family:Arial;font-size:14pt;line-height:120
%"> </font><br>');
document.write('<font id="calendarClock3" style="font-family:宋
体;font-size:9pt;line-height:120%"> </font><br>');
document.write('<font id="calendarClock4"
style="color:#100080;font-family:宋
体;font-size:8pt;line-height:120%"><b> </b></font>');
document.write('</td></tr></table>');
document.write('</td></tr></table>');
document.write('</td></tr></table>');
setInterval('refreshCalendarClock()',1000);
</SCRIPT>
<script language="javascript">
<!--
function makearray(size)
{
this.length=size;
for(i=1;i<=size;i++)
{
this[i]=0
}
return this;
}
msg=new makearray(3)
msg[1]="你好,欢迎使用学生课绩管理系统!!!"
msg[2]="请您选择用户类新,输入正确的用户名,密码!!";
msg[3]="谢谢您的使用!!!"
interval = 100;
seq = 0;
i=1;
function Scroll() {
document.tmForm.tmText.value = msg[i].substring(0, seq+1);
seq++;
if ( seq >= msg[i].length ) { seq = 0 ;i++;interval=900};
if(i>3){i=1};
window.setTimeout("Scroll();", interval );interval=100
} ;
//-->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312";
charset=gb2312">
<title>登陆</title>
<SCRIPT Language = javascript>
<!--
//下面的副程序将执行资料检查
function isValid()
{
//下面的if判断语句将检查是否输入帐号资料
if(frmLogin.id.value == "")
{
window.alert("您必须完成帐号的输入!");
//显示错误信息

document.frmLogin.elements(0).focus();
//将光标移至帐号输入栏 
return false;
}
//下面的if判断语句将检查是否输入帐号密码
if(frmLogin.password.value == "")
{
window.alert("您必须完成密码的输入!");
//显示错误信息
document.frmLogin.elements(1).focus();
//将光标移至密码输入栏
return false; //离开函数
}
frmLogin.submit(); //送出表单中的资料
}
-->
</SCRIPT>

 

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="#0099FF" OnLoad="Scroll()">
<form name="tmForm">
<input type="Text" name="tmText" size="40">
</form>
<p>
<%
String getmessage = (String) session.getAttribute("error");
if (getmessage==null) {getmessage="";} 
%>
<p1><font color="red"><%=getmessage%></font></p1></p>
<p align="center"><font color="#33FF00" size="+4" face="华文行楷">学生课
绩管理系统</font></p>
<form name="frmLogin" method="post"
action="http://localhost:8080/myapp/login_confirm" onSubmit="return
isValid(this);">
<p>
<div align="center">
<table width="47%" height="232" border=1 align="center" >
<tr >
<td height="44" colspan="2">
<div align="center"><font color="#FFFFFF" size="+2" face="华文行楷">请你
输入</font></div></td>
</tr>
<tr >
<td><div align="center"><font color="#FFFFFF"><strong>用户
</strong></font><font color="#FFFFFF"><strong>:
</strong></font></div></td>
<td><input name="kind" type="radio" value="student" checked >
<font color="#FFFFFF" size="+2" face="华文行楷">学生 </font>
<input type="radio" name="kind" value="teacher">
<font color="#FFFFFF" size="+2" face="华文行楷"> 教师 </font>
<input type="radio" name="kind" value="admin">
<font color="#FFFFFF" size="+2" face="华文行楷">管理员
</font></td>
</tr>
<tr >
<td width="27%"><div align="center"><strong><font
color="#FFFFFF">登陆名</font><font color="#FFFFFF">:
</font></strong></div></td>
<td width="73%"><input name="id" type="text" id="id" size="20"
maxlength="20"></td>
</tr>
<tr>
<td><div align="center"><strong> <font color="#FFFFFF">密码:
</font></strong></div></td>
<td><input name="password" type="password" id="password"
size="8" maxlength="8"></td>
</tr>
<tr >
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="登陆">
</div></td>
</tr>
</table>
<table>
</table> 
</div>
</form>
</body>


</html>


 

 




 

------------------------------------------------------------------------------

 

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);
			}
			}
			
			
}
		


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

没有更多推荐了,返回首页