编程模仿MySql客服端

写在前面

通过自己编写的Java代码程序,去模仿实现MySql客服端的简单功能,最终以控制台操作,很像在Dos窗口通过命令操作MySql数据库。

关键问题

在编写过程中遇到的一些小问题和一些值得留心注意的点:

  • 增删改查结果是否成功的获得
  • 查询结果 不定集列数的获得
1  //获取结果集的列数
2 78         ResultSetMetaData rsmd = rs.getMetaData();
3 79         int i = rsmd.getColumnCount();
  • 完整代码
 1 import java.sql.*;
 2 import java.util.*;
 3 
 4 public class YouSqlClient {
 5 
 6     private static String name = "";
 7     private static String passwor = "";
 8     static Connection con;
 9     static Statement statement = null;
10     static ResultSet rs;
11     static Scanner input = new Scanner(System.in);
12     
13     public static void main(String[] args) {
14         
15         System.out.println("userName:");
16         name = input.nextLine();
17         System.out.println("password:");
18         passwor = input.nextLine();
19         
20         try {
21             
22             String sql = "";
23             
24             Class.forName("com.mysql.jdbc.Driver").newInstance();//初始化数据库驱动
25             //指定链接数据库的信息
26             con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydbone?useUnicode=true&characterEncoding=UTF-8",name,passwor);
27             statement = con.createStatement();
28             System.out.println("login succeded!");//上面的语句成功执行,提示登陆成功
29             
30             boolean isExit = true;
31             
32             while(isExit) {//通过循环不断接收命令
33                 
34                 System.out.println("Please enter the commend>");
35                 
36                 //登陆后的操作
37                 sql = input.nextLine();//测试:show databases;
38                 
39                 try {
40                     if( sql.indexOf( "use" ) == 0 ) {//换数据库
41                         statement.executeQuery( sql );
42                         System.out.println("Database changed!");
43                     }
44                     else if ( ( sql.indexOf( "show" ) == 0 || sql.indexOf( "select" ) == 0) && statement.execute(sql) ) { //查表
45                         selectOperate();
46                     }
47                     else if ( statement.execute(sql) ) {//不是对表内数据的增删该查,不是数据库的切换,不是show table或者 DB
48                         //建立新表 + 删除表 + 等等其他操作
49                     }
50                     else if (sql.equals("exit")) {
51                         isExit = false;
52                         System.out.println("exit succeded!");
53                     }
54                     else {
55                         System.out.println( "query succedes!" );
56                     }
57                 } catch (SQLException e) {
58                     e.printStackTrace();
59                 }
60             }        
61         } catch (InstantiationException e) {
62             e.printStackTrace();
63         } catch (IllegalAccessException e) {
64             e.printStackTrace();
65         } catch (ClassNotFoundException e) {
66             e.printStackTrace();
67         } catch (SQLException e) {
68             e.printStackTrace();
69         }
70     }
71     
72     //查询
73     static void selectOperate() throws SQLException {
74         
75         rs = statement.getResultSet();
76         
77         //获取结果集的列数
78         ResultSetMetaData rsmd = rs.getMetaData();
79         int i = rsmd.getColumnCount();
80         
81         //列名
82         for( int j = 1; j <= i; j++ ) {
83             
84             System.out.print(rsmd.getColumnName(j) + "\t");
85         }
86         System.out.println("\n----------------------------------------------");
87         
88         while( rs.next() ) {    //    依次获得查询结果,每次执行nest,换一行
89         
90             for( int j = 1; j <= i; j++ ) {
91                 
92                 System.out.print(rs.getString(j) + "\t");
93             }
94             System.out.println();
95         }
96     }
97     
98 }

 

posted @ 2018-08-20 19:28  四叶笔记  阅读(280)  评论(0编辑  收藏  举报