11package zuo .biao .apijson .server .sql ;
22
3- import java .sql .Connection ;
4- import java .sql .DatabaseMetaData ;
5- import java .sql .DriverManager ;
6- import java .sql .ResultSet ;
7- import java .sql .Statement ;
3+ import java .sql .*;
84import java .util .ArrayList ;
95import java .util .List ;
106
@@ -21,37 +17,67 @@ public static void main(String[] args){
2117// System.out.println(TAG + JSON.toJSONString(select("stu")));
2218 }
2319
24- public static Connection getConnection () throws Exception {
20+ private static Connection connection ;
21+ private static Statement statement ;
22+ private static DatabaseMetaData metaData ;
23+ private SelectTable2 () {
24+ }
25+
26+ private static SelectTable2 instance ;
27+ public static synchronized SelectTable2 getInstance () {
28+ if (instance == null ) {
29+ instance = new SelectTable2 ();
30+ }
31+ return instance ;
32+ }
33+
34+
35+ public Connection getConnection () throws Exception {
2536 //调用Class.forName()方法加载驱动程序
2637 Class .forName ("com.mysql.jdbc.Driver" );
2738 System .out .println (TAG + "成功加载MySQL驱动!" );
2839 String url ="jdbc:mysql://localhost:3306/sys" ; //JDBC的URL
2940 return DriverManager .getConnection (url , "root" , "199531tommy" );
3041 }
3142
32-
33- public static JSONObject select (String table ) {
34- return select (new QueryConfig (table ));
43+ public void close () {
44+ try {
45+ statement .close ();
46+ connection .close ();
47+ } catch (SQLException e ) {
48+ e .printStackTrace ();
49+ }
50+ metaData = null ;
51+ statement = null ;
3552 }
36- public static JSONObject select (QueryConfig config ) {
53+
54+ public JSONObject select (QueryConfig config ) {
3755 if (config == null || StringUtil .isNotEmpty (config .getTable (), true ) == false ) {
3856 System .out .println (TAG + "select config==null||StringUtil.isNotEmpty(config.getTable(), true)==false>>return null;" );
3957 return null ;
4058 }
59+ final String sql = config .getSQL ();
60+
4161 try {
42- Connection conn = getConnection ();
43- Statement stmt = conn . createStatement (); //创建Statement对象
62+
63+
4464 System .out .println (TAG + "成功连接到数据库!" );
4565
46- List <String > list = getColumnList (config .getTable (), conn .getMetaData ());
66+ if (connection == null || connection .isClosed ()) {
67+ System .out .println (TAG + "select connection " + (connection == null ? " = null" : ("isClosed = " + connection .isClosed ()))) ;
68+ connection = getConnection ();
69+ statement = connection .createStatement (); //创建Statement对象
70+ metaData = connection .getMetaData ();
71+ }
72+
73+ List <String > list = getColumnList (config .getTable ());
4774 if (list == null || list .isEmpty ()) {
4875 return null ;
4976 }
5077
51- String sql = "select * from " + config .getTable () + config .getWhereString () + config .getLimitString (); //要执行的SQL
5278 System .out .println (TAG + "select sql = " + sql );
53-
54- ResultSet rs = stmt .executeQuery (sql );//创建数据对象
79+
80+ ResultSet rs = statement .executeQuery (sql );//创建数据对象
5581
5682 JSONObject object = null ;//new JSONObject();//null;
5783 int position = -1 ;
@@ -74,8 +100,6 @@ public static JSONObject select(QueryConfig config) {
74100 }
75101
76102 rs .close ();
77- stmt .close ();
78- conn .close ();
79103
80104 return object ;
81105 } catch (Exception e ) {
@@ -90,11 +114,11 @@ public static JSONObject select(QueryConfig config) {
90114 * @param table
91115 * @return
92116 */
93- public static List <String > getColumnList (String table , DatabaseMetaData meta ) {
117+ public List <String > getColumnList (String table ) {
94118 List <String > list = new ArrayList <String >();
95119 ResultSet rs ;
96120 try {
97- rs = meta .getColumns ("sys" , null , table , "%" );
121+ rs = metaData .getColumns ("sys" , null , table , "%" );
98122 while (rs .next ()) {
99123 System .out .println (TAG + rs .getString (4 ));
100124 list .add (rs .getString (4 ));
0 commit comments