廖井涛
2025-11-28 67f0be5a1d634ba3274fa9366ceacc3580f056b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.northglass.util;
 
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
 
 
 
public class DBHelper{
    
    private String sqlurl="jdbc:mariadb://localhost/gmms";
    private String user="root";
    private String password=null;
    
    
    public  String InvokeSqlProc(String name,String param1,String param2){
          Connection con=null;
          try{
                  con=DriverManager.getConnection(sqlurl,user,password);
                CallableStatement c=con.prepareCall(String.format("{call %s(?,?,?)}",name));
                c.setString(1,param1);
                c.setString(2,param2);
    
                c.registerOutParameter(3, java.sql.Types.VARCHAR);
                System.out.println("ok");
                c.execute(); 
                System.out.println("ok1");
                String message=c.getString(3);
                c.close();
                con.close();
                return message;
          }
          catch(Exception e){
                e.printStackTrace();
              if(con!=null)
                try {
                    con.close();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
              return "fail";
          }
        
    }
    
    
 
    
 
    //从数据库表中挑出一个字段作为前台select 的选项
    public  String MakeSelectOptionBySql(String sql,String field){
        QueryResult query=null;
           try{ 
              query=new QueryResult(sqlurl,user,password,sql);
              ResultSet result=query.Result;
            StringBuilder sb=new StringBuilder();
            while(result.next()){
                Object o=result.getObject(field);
                if(o!=null)                    
               sb.append(String.format("<option value=\"%s\">%s</option>\r\n",o.toString(),o.toString()));
            }
            query.Close();
            return sb.toString();
           }
           catch(Exception e){
               if(query!=null)
                   query.Close();
               return null;
           }
    }
    
    
    public QueryResult getQueryResult(String sql){
        QueryResult query=null;
        try{
         query=new QueryResult(sqlurl,user,password,sql);
        return query;
        }
        catch(Exception e){
            if(query!=null)
                query.Close();
            e.printStackTrace();
            return null;
        }
    }
    
    
    //从数据库表生成一个前台的表格tbody的行内容,PKName是主键字段,ShowField是要显示的列 其中#开头的字符串直接作为t内容填入
    public  String MakeHTMLTableRowsBySql(String sql,String PKName,String[] ShowField) throws SQLException{
        QueryResult query=null;
       try{ 
          query=new QueryResult(sqlurl,user,password,sql);
          ResultSet result=query.Result;
        StringBuilder sb=new StringBuilder();
        int[] fieldIndex=new int[ShowField.length];
        for(int i=0;i<ShowField.length;i++){
             if(ShowField[i].charAt(0)=='#'){
                 fieldIndex[i]=-100;
                 ShowField[i]=ShowField[i].substring(1);
             }
             else
                 fieldIndex[i]=result.findColumn(ShowField[i]);
        }
        //sb.append("<tbody>\r\n");
        while(result.next()){
            if(PKName!=null){
            sb.append(String.format("<tr data-id=\"%s\">\r\n",result.getObject(PKName).toString().trim()));
            }
            else
                sb.append("<tr>\r\n");
            for(int i=0;i<fieldIndex.length;i++){
                sb.append("<td>");
                int idx=fieldIndex[i];
                if(idx==-100){
                    sb.append(ShowField[i]);
                }
                else{
                
                    Object o=result.getObject(idx);
                    if(o!=null)
                        sb.append(o);    
                }
                sb.append("</td>");
            }
            sb.append("</tr>\r\n");
        }
        //sb.append("</tbody>\r\n");
        query.Close();
        return sb.toString();
       }
       catch(Exception e){
           if(query!=null)
               query.Close();
           e.printStackTrace();
           return null;
       }
    }
    
    public DBHelper(String sqlurl,String user,String password){
        this.sqlurl=sqlurl;
        this.user=user;
        this.password=password;
    }
    
    
    public  class QueryResult{
         private java.sql.Connection con;
         private java.sql.Statement statement;
         public java.sql.ResultSet Result;
           
         public  QueryResult(String conString,String user,String password,String Sql) throws SQLException{
             this.con= java.sql.DriverManager.getConnection(conString,user,password);
             this.statement=con.createStatement();
             this.Result=this.statement.executeQuery(Sql);
         }
         
 
         
         
         
         public void Close(){
             try{
             if(Result!=null){
                 Result.close();
                 Result=null;
             }
             }catch(Exception e){
                 
             }
             try{
                 if(statement!=null){
                     statement.close();
                     statement=null;
                 }
                 }catch(Exception e){
                     
                 }
             try{
                 if(con!=null){
                     con.close();
                     con=null;
                 }
                 }catch(Exception e){
                     
                 }
         }
        }
 
        
}