zoukankan      html  css  js  c++  java
  • 学生管理系统利用数据库优化,优化再优化!!

    1,        先通过批量提交的方法在数据库中初始化一些数据

    2,    package com.xt.java.base24;
    34,    import java.sql.Connection;
    5,    import java.sql.DriverManager;
    6,    import java.sql.SQLException;
    7,    import java.sql.Statement;
    89,    import org.junit.Before;
    10,    import org.junit.Test;
    1112public class BatchTest {
    13,        Connection conn=null;
    14,        Statement stat=null;
    151617,        @Before
    18public void beforeTest(){
    1920,            String url="jdbc:mysql://localhost:3306/lyxdatabases?useunicode=true&characterEncoding=utf-8";
    21try {
    22,                Class.forName("com.mysql.jdbc.Driver");
    23,                conn=DriverManager.getConnection(url, "root", "1234");
    24,                stat=conn.createStatement();
    2526,            } catch (ClassNotFoundException e) {
    27,                e.printStackTrace();
    28,            } catch (SQLException e) {
    29,                e.printStackTrace();
    30,            }
    3132,        }
    3334,        @Test
    3536public void testTest(){
    37/**
    38,             * 使用批量提交的方法
    39,             */
    40,            String sql1="insert into student2 values('20160800612','贾一','男',19,90)";
    41,            String sql2="insert into student2 values('20160800613','戚二','女',18,98)";
    42,            String sql3="insert into student2 values('20160800614','张三','男',20,67)";
    43,            String sql4="insert into student2 values('20160800615','赵四','男',17,87)";
    44,            String sql5="insert into student2 values('20160800616','王五','女',19,78)";
    45,            String sql6="insert into student2 values('20160800617','薛六','女',21,90)";
    46,            String sql7="insert into student2 values('20160800618','赵七','男',23,91)";
    47484950try {
    5152//将sql语句放入batch缓冲器中
    53,                stat.addBatch(sql1);
    54,                stat.addBatch(sql2);
    55,                stat.addBatch(sql3);
    56,                stat.addBatch(sql4);
    57,                stat.addBatch(sql5);
    58,                stat.addBatch(sql6);
    59,                stat.addBatch(sql7);
    60/**
    61,                 * 执行batch中的所有语句
    62,                 */
    63,                stat.executeBatch();
    64,                System.out.println("数据存入成功!!");
    65,            } catch (SQLException e) {
    66// TODO Auto-generated catch block
    67,                e.printStackTrace();
    68,            }
    6970try{
    71if(stat!=null){
    72,                    stat.close();
    73,                }
    74if(conn!=null){
    75,                    conn.close();
    76,                }
    77,            }catch(Exception e){
    78,                e.printStackTrace();
    79,            }
    8081,        }
    8283,    }

    2.进行操作

      1 package com.xt.java.base24;
      2 
      3 
      4 
      5 import java.sql.Connection;
      6 import java.sql.DriverManager;
      7 import java.sql.ResultSet;
      8 import java.sql.SQLException;
      9 import java.sql.Statement;
     10 import java.util.Scanner;
     11 
     12 public class StudentManager {
     13 
     14     public static void main(String[] args) {
     15         while(true){
     16         Scanner scanner=new Scanner(System.in);
     17         System.out.println("----------------欢迎进入学生管理系统-------------------");
     18         System.out.println("插入----------1");
     19         System.out.println("删除----------2");
     20         System.out.println("修改----------3");
     21         System.out.println("查询----------4");
     22         System.out.println("退出----------0
    
    ");
     23         System.out.println("请选择您要进行的操作:");
     24         int selectNum=scanner.nextInt();
     25         Connection conn=null;
     26         Statement stat=null;
     27         ResultSet rs=null;
     28         String url="jdbc:mysql://localhost:3306/lyxdatabases";
     29         
     30         //加载驱动,通过驱动管理器将Java与数据库建立联系。同时排除异常。
     31         try {
     32             Class.forName("com.mysql.jdbc.Driver");
     33             conn=DriverManager.getConnection(url,"root","1234");
     34             stat=conn.createStatement();
     35         } catch (ClassNotFoundException e) {
     36             e.printStackTrace();
     37         } catch (SQLException e) {
     38             e.printStackTrace();
     39         }
     40         
     41         
     42         switch(selectNum){
     43         case 0:{
     44             System.out.println("退出系统!!!");
     45             System .exit(0);
     46             break;
     47         }
     48         case 1:{
     49             System.out.println("请输入您要插入的学生的学号:");
     50             String stuNum1=scanner.next();
     51             System.out.println("请输入您要插入的学生的姓名:");
     52             String stuName1=scanner.next();
     53             System.out.println("请输入您要插入的学生的性别:");
     54             String gender1=scanner.next();
     55             System.out.println("请输入您要插入的学生的年龄:");
     56             int age1=scanner.nextInt();
     57             System.out.println("请输入您要插入的学生的分数:");
     58             double score1=scanner.nextDouble();
     59             String sql="insert into student2 values('"+stuNum1+"','"+stuName1+"','"+gender1+"',"+age1+","+score1+")";
     60             //查看sql语句是否输入正确!
     61             System.out.println(sql);
     62             try {
     63                 int affectedRows=stat.executeUpdate(sql);
     64                 if(affectedRows>0){
     65                     System.out.println("学生插入成功!");
     66                 }else{
     67                     System.out.println("学生插入失败,请重新操作。。。。。。");
     68                 }
     69             } catch (SQLException e) {
     70                 // TODO Auto-generated catch block
     71                 e.printStackTrace();
     72             }
     73             try {
     74                 if(rs!=null){
     75                 rs.close();
     76                 }
     77                 if(stat!=null){
     78                     stat.close();
     79                 }
     80                 if(conn!=null){
     81                     conn.close();
     82                 }
     83             }
     84              catch (SQLException e) {
     85                 // TODO Auto-generated catch block
     86                 e.printStackTrace();
     87              
     88             }
     89             break;
     90         }
     91         case 2:{
     92             System.out.println("请输入您要删除的学生的学号:");
     93             String stuNum2=scanner.next();
     94             String sql2="delete from student2 where stuNum='"+stuNum2+"'";
     95             System.out.println(sql2);
     96             try {
     97                 int affectedRows1=stat.executeUpdate(sql2);
     98                 if(affectedRows1>0){
     99                     System.out.println("学删除成功!!");
    100                 }else{
    101                     System.out.println("学生删除失败,请重新操作。。。。。。");
    102                 }
    103             } catch (SQLException e) {
    104                 // TODO Auto-generated catch block
    105                 e.printStackTrace();
    106             }
    107             try {
    108                 if(rs!=null){
    109                 rs.close();
    110                 }
    111                 if(stat!=null){
    112                     stat.close();
    113                 }
    114                 if(conn!=null){
    115                     conn.close();
    116                 }
    117             }
    118              catch (SQLException e) {
    119                 // TODO Auto-generated catch block
    120                 e.printStackTrace();
    121              
    122             }
    123             break;
    124         }
    125         case 3:{
    126             while (true){
    127             System.out.println("请输入您要改正的学生的学号:");
    128             String stuNum=scanner.next();
    129             System.out.println("学号----------a");
    130             System.out.println("名字----------b");
    131             System.out.println("性别----------c");
    132             System.out.println("年龄----------d");
    133             System.out.println("成绩----------e");
    134             System.out.println("操作完成,退出系统----------f");
    135             
    136             System.out.println("----------请选择您要改正的对象:");
    137             String mark=scanner.next();
    138             /**
    139              * 改正编号。。。
    140              */
    141             if(mark.equals("a")){
    142                 System.out.println("请输入你要改正为的学号:");
    143                 String stuNum3=scanner.next();
    144                 String sql2="update student2 set stuNum='"+stuNum3+" 'where stuNum='"+stuNum+"'";
    145                 //查看sql2语句是否输入正确!
    146                 System.out.println(sql2);
    147                 
    148                 try {
    149                     int affectedRows1=stat.executeUpdate(sql2);
    150                     if(affectedRows1>0){
    151                         System.out.println("学生改正成功!!");
    152                     }else{
    153                         System.out.println("学生改正失败,请重新操作。。。。。。");
    154                     }
    155                 } catch (SQLException e) {
    156                     // TODO Auto-generated catch block
    157                     e.printStackTrace();
    158                 }
    159             
    160             }
    161             
    162             /**
    163              * 改正名称。。。。
    164              */
    165             else if(mark.equals("b")){
    166                 System.out.println("请输入你要改正的姓名:");
    167                 String stuName3=scanner.next();
    168                 String sql3="update student2 set name='"+stuName3+"' where stuNum='"+stuNum+"'";
    169                 //查看sql3语句是否输入正确!
    170                 System.out.println(sql3);
    171                 
    172                 try {
    173                     int affectedRows1=stat.executeUpdate(sql3);
    174                     if(affectedRows1>0){
    175                         System.out.println("学生改正成功!!");
    176                     }else{
    177                         System.out.println("学生改正失败,请重新操作。。。。。。");
    178                     }
    179                 } catch (SQLException e) {
    180                     // TODO Auto-generated catch block
    181                     e.printStackTrace();
    182                 }
    183             
    184             }
    185             /**
    186              * 改正数量。。。。
    187              */
    188             else if(mark.equals("c")){
    189                 System.out.println("请输入你要改正的性别:");
    190                 String gender3=scanner.next();
    191                 String sql4="update student2 set gender='"+gender3+"' where stuNum='"+stuNum+"'";
    192                 //查看sql4语句是否输入正确!
    193                 System.out.println(sql4);
    194                 
    195                 try {
    196                     int affectedRows1=stat.executeUpdate(sql4);
    197                     if(affectedRows1>0){
    198                         System.out.println("学生改正成功!!");
    199                     }else{
    200                         System.out.println("学生改正失败,请重新操作。。。。。。");
    201                     }
    202                 } catch (SQLException e) {
    203                     // TODO Auto-generated catch block
    204                     e.printStackTrace();
    205                 }
    206                 try {
    207                     if(rs!=null){
    208                     rs.close();
    209                     }
    210                     if(stat!=null){
    211                         stat.close();
    212                     }
    213                     if(conn!=null){
    214                         conn.close();
    215                     }
    216                 }
    217                  catch (SQLException e) {
    218                     // TODO Auto-generated catch block
    219                     e.printStackTrace();
    220                  
    221                 }
    222             
    223             }
    224             /**
    225              * 改正价钱。。。
    226              */
    227             else if(mark.equals("d")){
    228                 System.out.println("请输入你要改正的年龄:");
    229                 int age3=scanner.nextInt();
    230                 String sql5="update student2 set age="+age3+" where stuNum='"+stuNum+"'";
    231                 //查看sql4语句是否输入正确!
    232                 System.out.println(sql5);
    233                 
    234                 try {
    235                     int affectedRows1=stat.executeUpdate(sql5);
    236                     if(affectedRows1>0){
    237                         System.out.println("学生改正成功!!");
    238                     }else{
    239                         System.out.println("学生改正失败,请重新操作。。。。。。");
    240                     }
    241                 } catch (SQLException e) {
    242                     // TODO Auto-generated catch block
    243                     e.printStackTrace();
    244                 }
    245             
    246             }
    247             else if(mark.equals("e")){
    248                 System.out.println("请输入你要改正的成绩:");
    249                 double score3=scanner.nextDouble();
    250                 String sql6="update student2 set score="+score3+" where stuNum='"+stuNum+"'";
    251                 //查看sql5语句是否输入正确!
    252                 System.out.println(sql6);
    253                 
    254                 try {
    255                     int affectedRows1=stat.executeUpdate(sql6);
    256                     if(affectedRows1>0){
    257                         System.out.println("学生改正成功!!");
    258                     }else{
    259                         System.out.println("学生改正失败,请重新操作。。。。。。");
    260                     }
    261                 } catch (SQLException e) {
    262                     // TODO Auto-generated catch block
    263                     e.printStackTrace();
    264                 }
    265             }
    266             else if(mark.equals("f")){
    267                 System.out.println("操作完成,退出系统!!");
    268                 return;
    269             }
    270             else{
    271                 System.out.println("您输入的数据有错误,请重新输入!!!");
    272                 break;
    273             }
    274             
    275             
    276         }
    277             
    278         }
    279         case 4:{
    280             System.out.println("请输入您要查询的学生的学号:");
    281             String stuNum4=scanner.next();
    282             String sql7="select* from student2 where stuNum='"+stuNum4+"'";
    283             System.out.println(sql7);
    284             try {
    285                 rs=stat.executeQuery(sql7);
    286                 while(rs.next()){
    287                 System.out.print("学号:"+rs.getString("stuNum")+"    ");
    288                 System.out.print("姓名:"+rs.getString("name")+"   ");
    289                 System.out.print("性别:"+rs.getString("gender")+"   ");
    290                 System.out.print("年龄:"+rs.getInt("age"));}
    291                 System.out.println("成绩:"+rs.getDouble("score"));
    292             } catch (SQLException e) {
    293                 // TODO Auto-generated catch block
    294                 e.printStackTrace();
    295             }
    296         
    297         
    298         }
    299         
    300         try {
    301             if(rs!=null){
    302             rs.close();
    303             }
    304             if(stat!=null){
    305                 stat.close();
    306             }
    307             if(conn!=null){
    308                 conn.close();
    309             }
    310         }
    311          catch (SQLException e) {
    312             // TODO Auto-generated catch block
    313             e.printStackTrace();
    314          
    315         }
    316         break;
    317         }
    318         
    319         
    320         }
    321         
    322     }
    323 
    324 }
  • 相关阅读:
    CTreeCtrl::HitTest
    GetLastError()函数返回值及含义
    最大轮廓和投影 转
    一些Python黑客脚本
    win10系统架构调用
    rootkit基础
    面向对象编程
    机器学习概述
    XXE攻击
    浏览器安全
  • 原文地址:https://www.cnblogs.com/lyxcode/p/7388990.html
Copyright © 2011-2022 走看看