Chinaunix首页 | 论坛 | 博客
  • 博客访问: 420137
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Java

2015-03-04 21:49:21


点击(此处)折叠或打开

  1. package cn.com.xiebiao.jdbcTest;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Scanner;
  9. /**
  10.  *
  11.  * Title : JdbcStudents.java
  12.  * Company: ZhenBot
  13.  * Author : Vibe Xie @
  14.  * Time : Mar 4, 2015 9:34:54 PM
  15.  * Copyright: Copyright (c) 2015
  16.  * Description:学生管理系统v1(mysql实现)
  17.  *
  18.  * ******Statement可以优化为prepareStatement与编译sql语句。
  19.  *
  20.  */
  21. public class JdbcStudents {    
  22.     public static Connection connection;
  23.     public static Statement statement;
  24.     public static ResultSet resultSet;
  25.     public static PreparedStatement preparedStatement;
  26.     
  27.     static final String url="jdbc:mysql://localhost:3306/xiebiaoDB";
  28.     static final String user="xiebiao";
  29.     static final String password="*********";
  30.     
  31.     public static String sql;

  32.     public static Scanner in=new Scanner(System.in);
  33.     
  34.     /*静态代码块,加载jdbc和连接Mysql数据库*/
  35.     static{
  36.         try {
  37.             Class.forName("com.mysql.jdbc.Driver");
  38.         } catch (ClassNotFoundException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         }
  42.         
  43.         try {
  44.             connection=DriverManager.getConnection(url, user, password);
  45.             if(connection!=null){
  46.                 System.out.println("连接:"+url+"成功");
  47.             }else {
  48.                 System.out.println("连接:"+url+"失败");
  49.                 quit();
  50.             }
  51.         } catch (SQLException e) {
  52.             // TODO Auto-generated catch block
  53.             e.printStackTrace();
  54.         }
  55.     }
  56.     
  57.     /*销毁资源*/
  58.     public static void destoryResource(){
  59.         try {
  60.             if(resultSet!=null){
  61.                 resultSet.close();
  62.                 resultSet=null;
  63.             }
  64.             
  65.             if(statement!=null){
  66.                 statement.close();
  67.                 statement=null;
  68.             }
  69.             
  70.             if(connection!=null){
  71.                 connection.close();
  72.                 connection=null;
  73.             }
  74.             
  75.         } catch (SQLException e) {
  76.             // TODO Auto-generated catch block
  77.             e.printStackTrace();
  78.         }
  79.     }
  80.     
  81.     public static void add(){
  82.         int addId;
  83.         String addName;
  84.         String addClass;
  85.         
  86.         System.out.print("请输入Id:");
  87.         addId=in.nextInt();
  88.         System.out.print("请输入Name:");
  89.         addName=in.next();
  90.         System.out.print("请输入Class:");
  91.         addClass=in.next();
  92.         
  93.         try{
  94.             statement=connection.createStatement();
  95.             sql="insert into students values ("+addId+",'"+addName+"','"+addClass+"');";
  96.             int result=statement.executeUpdate(sql);
  97.             if(result>0){
  98.                 System.out.println("添加成功");
  99.             }else {
  100.                 System.out.println("添加失败");
  101.             }
  102.         }catch(Exception ex){
  103.             ex.printStackTrace();
  104.         }
  105.     }
  106.     
  107.     public static void delete(){
  108.         int choice;
  109.         int deleteId;
  110.         String deleteName;
  111.         String deleteClass;
  112.         
  113.         System.out.printf("1-按Id删除\n"
  114.                  + "2-按Name删除\n"
  115.                  + "3-按Class删除\n"
  116.                  + "请输入选项:");
  117.         choice=in.nextInt();
  118.         
  119.         switch (choice) {
  120.         case 1:
  121.             System.out.print("请输入Id:");
  122.             deleteId=in.nextInt();
  123.             sql="delete from students where id="+deleteId+";";
  124.             break;
  125.         case 2:
  126.             System.out.print("请输入Name:");
  127.             deleteName=in.next();
  128.             sql="delete from students where name='"+deleteName+"';";
  129.             break;
  130.         case 3:
  131.             System.out.print("请输入Class:");
  132.             deleteClass=in.next();
  133.             sql="delete from students where class='"+deleteClass+"';";
  134.             break;
  135.         default:
  136.             break;
  137.         }
  138.             
  139.         try{
  140.             statement=connection.createStatement();
  141.             int result=statement.executeUpdate(sql);
  142.             if(result>0){
  143.                 System.out.println("删除成功");
  144.             }else {
  145.                 System.out.println("删除失败");
  146.             }
  147.             
  148.         }catch(Exception ex){
  149.             
  150.         }
  151.     }
  152.     
  153.     public static void alter(){
  154.         int id;
  155.         int alterId;
  156.         String alterName;
  157.         String alterClass;
  158.         
  159.         System.out.print("请输入要修改的Id:");
  160.         id=in.nextInt();
  161.         System.out.print("开始修改!\n");
  162.         System.out.print("请输入Id:");
  163.         alterId=in.nextInt();
  164.             
  165.         System.out.print("请输入Name:");
  166.         alterName=in.next();
  167.             
  168.         System.out.print("请输入Class:");
  169.         alterClass=in.next();
  170.         sql="update students set id="+alterId+",name='"+alterName+"',class='"+alterClass+"' where id="+id+";";
  171.             
  172.         try{
  173.             statement=connection.createStatement();
  174.             int result=statement.executeUpdate(sql);
  175.             if(result>0){
  176.                 System.out.println("修改成功");
  177.             }else {
  178.                 System.out.println("修改失败");
  179.             }
  180.             
  181.         }catch(Exception ex){
  182.             
  183.         }
  184.     }
  185.     
  186.     public static void query(){
  187.         int choice;
  188.         int queryId;
  189.         String queryName;
  190.         String queryClass;
  191.         
  192.         System.out.printf("1-按Id查询\n"
  193.                  + "2-按Name查询\n"
  194.                  + "3-按Class查询\n"
  195.                  + "请输入选项:");
  196.         choice=in.nextInt();
  197.         
  198.         switch (choice) {
  199.         case 1:
  200.             System.out.print("请输入Id:");
  201.             queryId=in.nextInt();
  202.             sql="select * from students where id="+queryId+";";
  203.             break;
  204.         case 2:
  205.             System.out.print("请输入Name:");
  206.             queryName=in.next();
  207.             sql="select * from students where name='"+queryName+"';";
  208.             break;
  209.         case 3:
  210.             System.out.print("请输入Class:");
  211.             queryClass=in.next();
  212.             sql="select * from students where class='"+queryClass+"';";
  213.             break;
  214.         default:
  215.             break;
  216.         }
  217.         
  218.         try{
  219.             statement=connection.createStatement();
  220.             resultSet=statement.executeQuery(sql);
  221.             
  222.             while(resultSet.next()){
  223.                 System.out.println("学号:"+resultSet.getInt("id")+" 姓名:"+resultSet.getString("name")+" 班級:"+resultSet.getString("class"));
  224.                 
  225.             }
  226.             
  227.         }catch(Exception ex){
  228.             ex.printStackTrace();
  229.         }
  230.     }
  231.     
  232.     public static void queryAll(){
  233.         sql="select * from students;";
  234.         try{
  235.             statement=connection.createStatement();
  236.             resultSet=statement.executeQuery(sql);
  237.             
  238.             while(resultSet.next()){
  239.                 System.out.println("学号:"+resultSet.getInt("id")+" 姓名:"+resultSet.getString("name")+" 班級:"+resultSet.getString("class"));
  240.                 
  241.             }
  242.             
  243.         }catch(Exception ex){
  244.             ex.printStackTrace();
  245.         }
  246.     }
  247.     
  248.     public static void quit(){
  249.         System.out.println("成功退出");
  250.         destoryResource();
  251.         System.exit(0);
  252.     }

  253.     public static void menu(){
  254.         System.out.printf("***********学生数据管理系统(Mysql实现)*************\n"
  255.                         + " 1-增加\n"
  256.                         + " 2-删除\n"
  257.                         + " 3-修改\n"
  258.                         + " 4-查询\n"
  259.                         + " 5-查询所有\n"
  260.                         + " 6-退出\n"
  261.                         + "************************************************\n"
  262.                         + "请输入选项:");
  263.     }
  264.     
  265.     public static void main(String[] args){
  266.         int choice;
  267.         while (true){
  268.             menu();
  269.             choice=in.nextInt();
  270.             switch (choice) {
  271.             case 1:
  272.                 add();
  273.                 break;
  274.             case 2:
  275.                 delete();
  276.                 break;
  277.             case 3:
  278.                 alter();
  279.                 break;
  280.             case 4:
  281.                 query();
  282.                 break;
  283.             case 5:
  284.                 queryAll();
  285.                 break;
  286.             default:
  287.                 quit();
  288.                 break;
  289.             }
  290.         }
  291.     }
  292. }


阅读(1337) | 评论(0) | 转发(0) |
0

上一篇:chrome插件

下一篇:eclipse快捷键

给主人留下些什么吧!~~