From 1d0a4978574e2b67b4ed04783465710978dbdaba Mon Sep 17 00:00:00 2001
From: wu <731351411@qq.com>
Date: 星期三, 19 三月 2025 09:17:56 +0800
Subject: [PATCH] 导入S7JAR包,更改数据库连接
---
nglib/src/builder/ThreadHttpServer.java | 8
gmms/WebContent/mysqlInsert/mysql_connect.jsp | 4
nglib/src/ng/db/DBSession.java | 695 +++++++++++------------
nglib/bin/META-INF/MANIFEST.MF | 1
nglib/src/builder/PaibanManager.java | 7
gmms/.classpath | 1
gmms/WebContent/login/bdg.jsp | 3
nglib/.idea/encodings.xml | 2
nglib/src/META-INF/MANIFEST.MF | 1
gmms/WebContent/login/index.jsp | 1
nglib/nglib.iml | 118 +++
gmms/.settings/org.eclipse.jdt.core.prefs | 10
nglib/src/builder/Manager.java | 58 +
nglib/src/builder/MachineManager.java | 16
nglib/src/ng/db/DBHelper.java | 779 +++++++++++++-------------
nglib/.classpath | 5
16 files changed, 913 insertions(+), 796 deletions(-)
diff --git a/gmms/.classpath b/gmms/.classpath
index 05b69ab..b80cf54 100644
--- a/gmms/.classpath
+++ b/gmms/.classpath
@@ -13,6 +13,5 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
- <classpathentry kind="lib" path="D:/Mes/s7connector-2.1.jar"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
diff --git a/gmms/.settings/org.eclipse.jdt.core.prefs b/gmms/.settings/org.eclipse.jdt.core.prefs
index 0c68a61..39f0fa0 100644
--- a/gmms/.settings/org.eclipse.jdt.core.prefs
+++ b/gmms/.settings/org.eclipse.jdt.core.prefs
@@ -1,7 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
-org.eclipse.jdt.core.compiler.compliance=1.8
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.source=1.8
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.1
+org.eclipse.jdt.core.compiler.compliance=1.3
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=ignore
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=ignore
+org.eclipse.jdt.core.compiler.source=1.3
diff --git a/gmms/WebContent/login/bdg.jsp b/gmms/WebContent/login/bdg.jsp
index 3b2dc2a..a62fcdb 100644
--- a/gmms/WebContent/login/bdg.jsp
+++ b/gmms/WebContent/login/bdg.jsp
@@ -6,6 +6,7 @@
response.setContentType("text/html; charset=utf-8");
%>
+<%@ page import="com.mysql.cj.jdbc.Driver" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
@@ -71,6 +72,6 @@
}
%>
- <iframe id='main' src='machinelistcontent.jsp?adminid="+adminid+"&xingming="+xingming+"&dengji="+dengji+"&anquanma="+anquanma+"&yuyan="+yuyan+"' style="width:87%;height:90%" frameborder='0'></iframe>
+ <iframe id='main' src='machinelistcontent.jsp?adminid="+adminid+"&xingming="+xingming+"&dengji="+dengji+"&anquanma="+anquanma+"&yuyan="+yuyan+"' style="width:87%;height:90%" frameborder='0'></iframe>
</body>
</html>
\ No newline at end of file
diff --git a/gmms/WebContent/login/index.jsp b/gmms/WebContent/login/index.jsp
index 8a81505..94b0cb6 100644
--- a/gmms/WebContent/login/index.jsp
+++ b/gmms/WebContent/login/index.jsp
@@ -2,6 +2,7 @@
<%@page import="javax.swing.text.html.HTML"%>
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
+
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
diff --git a/gmms/WebContent/mysqlInsert/mysql_connect.jsp b/gmms/WebContent/mysqlInsert/mysql_connect.jsp
index d1279ba..805f409 100644
--- a/gmms/WebContent/mysqlInsert/mysql_connect.jsp
+++ b/gmms/WebContent/mysqlInsert/mysql_connect.jsp
@@ -13,10 +13,10 @@
//杩炴帴鏁版嵁搴�
String ip="localhost:3306";
String driverClass = "com.mysql.cj.jdbc.Driver";
-String url="jdbc:mysql://"+ip+"/gmms_haier?serverTimezone=GMT%2B8";
+String url="jdbc:mysql://"+ip+"/haier?serverTimezone=GMT%2B8";
String user = "root";
String password = "beibo.123/";
-String url1 = "jdbc:mysql://"+ip+"/gmms";
+String url1 = "jdbc:mysql://"+ip+"/haier";
String urlglass="jdbc:mysql://"+ip+"/mesgglass?serverTimezone=GMT%2B8";
String url3="jdbc:mysql://"+ip+"/glog?serverTimezone=GMT%2B8";
diff --git a/nglib/.classpath b/nglib/.classpath
index e4a2f2a..98f106f 100644
--- a/nglib/.classpath
+++ b/nglib/.classpath
@@ -5,5 +5,10 @@
<classpathentry kind="lib" path="C:/Users/a'a'a/Desktop/Mes椤圭洰/repository/org/json/json/20080701/json-20080701.jar"/>
<classpathentry kind="lib" path="D:/zyz/web_optima/GERP/WebContent/WEB-INF/lib/mysql-connector-java-8.0.20.jar"/>
<classpathentry kind="lib" path="D:/zyz/web_optima/GERP/WebContent/WEB-INF/lib/jacob.jar"/>
+ <classpathentry kind="lib" path="E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/iot-communication-1.4.2.jar"/>
+ <classpathentry kind="lib" path="E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/jul-to-slf4j-1.7.28.jar"/>
+ <classpathentry kind="lib" path="E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/logback-classic-1.2.3.jar"/>
+ <classpathentry kind="lib" path="E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/logback-core-1.2.3.jar"/>
+ <classpathentry kind="lib" path="E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/slf4j-api-1.7.28.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
diff --git a/nglib/.idea/encodings.xml b/nglib/.idea/encodings.xml
index 51cb290..fdeccc0 100644
--- a/nglib/.idea/encodings.xml
+++ b/nglib/.idea/encodings.xml
@@ -4,6 +4,8 @@
<file url="file://$PROJECT_DIR$/src/builder/HttpHandlerDemo.java" charset="GBK" />
<file url="file://$PROJECT_DIR$/src/builder/MachineManager.java" charset="GBK" />
<file url="file://$PROJECT_DIR$/src/builder/Manager.java" charset="GBK" />
+ <file url="file://$PROJECT_DIR$/src/builder/ThreadHttpServer.java" charset="GBK" />
+ <file url="file://$PROJECT_DIR$/src/ng/db/DBSession.java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/toTcp/TCPClient.java" charset="GBK" />
<file url="PROJECT" charset="GBK" />
</component>
diff --git a/nglib/bin/META-INF/MANIFEST.MF b/nglib/bin/META-INF/MANIFEST.MF
index 9c0cd07..ad0782c 100644
--- a/nglib/bin/META-INF/MANIFEST.MF
+++ b/nglib/bin/META-INF/MANIFEST.MF
@@ -1,3 +1,4 @@
Manifest-Version: 1.0
+Main-Class: com.example.Main # 涓荤被鍏ㄩ檺瀹氬悕
Main-Class: builder.Manager
diff --git a/nglib/nglib.iml b/nglib/nglib.iml
index 215b874..747da8d 100644
--- a/nglib/nglib.iml
+++ b/nglib/nglib.iml
@@ -16,33 +16,131 @@
</content>
<orderEntry type="jdk" jdkName="JavaSE-1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
- <orderEntry type="module-library">
- <library name="json-20080701.jar">
+ <orderEntry type="module-library" exported="">
+ <library>
<CLASSES>
- <root url="file://C:/Users/a'a'a/Desktop/Mes椤圭洰/repository/org/json/json/20080701/json-20080701.jar" />
+ <root url="jar://$MODULE_DIR$/../json-20230618.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
- <orderEntry type="module-library">
- <library name="mysql-connector-java-8.0.20.jar">
+ <orderEntry type="module-library" exported="">
+ <library>
<CLASSES>
- <root url="file://$MODULE_DIR$/../../zyz/web_optima/GERP/WebContent/WEB-INF/lib/mysql-connector-java-8.0.20.jar" />
+ <root url="jar://$MODULE_DIR$/../mysql-connector-java-8.0.20.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
- <orderEntry type="module-library">
- <library name="jacob.jar">
+ <orderEntry type="module-library" exported="">
+ <library>
<CLASSES>
- <root url="file://$MODULE_DIR$/../../zyz/web_optima/GERP/WebContent/WEB-INF/lib/jacob.jar" />
+ <root url="jar://$MODULE_DIR$/../jacob-1.18.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
- <orderEntry type="library" name="fastjson-1.2.2" level="project" />
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://$MODULE_DIR$/../fastjson-1.2.2.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/iot-communication-1.4.2.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/jul-to-slf4j-1.7.28.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/logback-classic-1.2.3.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/logback-core-1.2.3.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/nglib.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://E:/HuaweiMoveData/Users/SNG-010/Desktop/娴峰皵浜屾湡/slf4j-api-1.7.28.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://$MODULE_DIR$/../druid-1.1.12.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://$MODULE_DIR$/../commons-net-3.9.0.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://$MODULE_DIR$/../artemis-http-client-1.1.10.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ <orderEntry type="module-library" exported="">
+ <library>
+ <CLASSES>
+ <root url="jar://$MODULE_DIR$/../iot-communication-1.4.2.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
</component>
</module>
\ No newline at end of file
diff --git a/nglib/src/META-INF/MANIFEST.MF b/nglib/src/META-INF/MANIFEST.MF
index 9c0cd07..ad0782c 100644
--- a/nglib/src/META-INF/MANIFEST.MF
+++ b/nglib/src/META-INF/MANIFEST.MF
@@ -1,3 +1,4 @@
Manifest-Version: 1.0
+Main-Class: com.example.Main # 涓荤被鍏ㄩ檺瀹氬悕
Main-Class: builder.Manager
diff --git a/nglib/src/builder/MachineManager.java b/nglib/src/builder/MachineManager.java
index 70ab82f..9f23c90 100644
--- a/nglib/src/builder/MachineManager.java
+++ b/nglib/src/builder/MachineManager.java
@@ -57,16 +57,18 @@
private item innerGetItem(String name){
for(int i=0;i<items.size();i++){
- if(name==items.get(i).Name)
+ if(name==items.get(i).Name){
return items.get(i);
+ }
}
return null;
}
public item innerGetItem(int id){
for(int i=0;i<items.size();i++){
- if(id==items.get(i).ID)
+ if(id==items.get(i).ID){
return items.get(i);
+ }
}
return null;
}
@@ -113,8 +115,9 @@
obj.put("count", it.serv.ioCount); //放入读取次数
send=it.serv.lastSend;
get=it.serv.lastGet;
- if(send==null)
- send="---";
+ if(send==null) {
+ send = "---";
+ }
if(get==null){
get="---";
}
@@ -127,8 +130,9 @@
obj.put("dev_state", it.serv.zhangtai);
String st="线程未开启";
if(it.serv!=null){
- if(it.serv.isRunning())
- st="未连接";
+ if(it.serv.isRunning()) {
+ st = "未连接";
+ }
if(it.serv.isOnLine()){
st="已连接";
}
diff --git a/nglib/src/builder/Manager.java b/nglib/src/builder/Manager.java
index 63687e0..2ef111f 100644
--- a/nglib/src/builder/Manager.java
+++ b/nglib/src/builder/Manager.java
@@ -1,18 +1,16 @@
package builder;
-import java.io.BufferedWriter;
-import java.io.OutputStreamWriter;
-import java.net.Socket;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
-import org.json.JSONArray;
-
+import S7.S7control;
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import ng.db.DBHelper;
import ng.db.DBSession;
import ng.devices.HexUtil;
+import org.json.JSONArray;
+
+import java.io.BufferedWriter;
+import java.io.OutputStreamWriter;
+import java.net.Socket;
+import java.sql.ResultSet;
public class Manager {
//设备管理器
@@ -84,31 +82,37 @@
//读取设备列表
DBHelper db=DBHelper.getDBHelper("mes");
sn=DBHelper.getDBHelper("mes").createSession(false);
- sn.createSql("select id,machine_name,ip_address,port,flag,read_size,read_offset,function_type,flag from machine");
+ sn.createSql("select id,machine_name,ip_address,port,flag,read_size,read_offset,function_type,flag from machine where id=78");
ResultSet set= sn.query().getCurrentResult();
- //创建所有设备通讯
- while(set.next()){
- int id=(int)set.getLong(1);
- String name=set.getString(2);
- String ip=set.getString(3);
- int port=set.getInt(4);
- int flag=set.getInt(5);
- int readSize=set.getInt(6);
- int readOffset=set.getInt(7);
- byte functionType=set.getByte(8);
- int flags=set.getInt(9);
- if(flags==1){
- mmgr.createDevice(db,id,name,ip,port,readOffset,readSize,null,functionType);
- }
+ System.out.println("S7链接");
+ S7control s7=new S7control(EPlcType.S1500, "192.168.30.14", 102, 0, 0);
+ System.out.println("S7:"+s7.readByte("DB34.0", 2));
- }
+// //创建所有设备通讯
+// while(set.next()){
+// int id=(int)set.getLong(1);
+// String name=set.getString(2);
+// String ip=set.getString(3);
+// int port=set.getInt(4);
+// int flag=set.getInt(5);
+// int readSize=set.getInt(6);
+// int readOffset=set.getInt(7);
+// byte functionType=set.getByte(8);
+// int flags=set.getInt(9);
+// if(flags==1){
+// mmgr.createDevice(db,id,name,ip,port,readOffset,readSize,null,functionType);
+// }
+//
+// }
}
catch(Exception e){
}
finally{
- if(sn!=null)
+ if(sn!=null){
+
sn.close();
+ }
}
mmgr.RunAll();
}
diff --git a/nglib/src/builder/PaibanManager.java b/nglib/src/builder/PaibanManager.java
index fb13e40..c5cee0c 100644
--- a/nglib/src/builder/PaibanManager.java
+++ b/nglib/src/builder/PaibanManager.java
@@ -2,7 +2,6 @@
import java.io.IOException;
import java.util.Properties;
-import ng.Algorithm.Layouts.*;
import ng.Algorithm.Layouts.AlgorithmDBInterface.ComputeResult;
import ng.db.NGFunction;
@@ -19,11 +18,9 @@
//初始化排版管理器
public void initPainbanManager(String url,String user,String password) throws IOException{
Properties properties = NGFunction.getProperties();
- String s= url+"&user="+
- user+"&password="+
- password;
+ String s= url+"&user="+user+"&password="+password;
System.out.println("l:"+s);
- ng.Algorithm.Layouts.AlgorithmDBInterface.initAlgorithm(s, lines);
+ ng.Algorithm.Layouts.AlgorithmDBInterface.initAlgorithm(s, lines);
}
diff --git a/nglib/src/builder/ThreadHttpServer.java b/nglib/src/builder/ThreadHttpServer.java
index 49be5e1..5a3e1f1 100644
--- a/nglib/src/builder/ThreadHttpServer.java
+++ b/nglib/src/builder/ThreadHttpServer.java
@@ -4,15 +4,21 @@
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-
+
+import S7.S7control;
+import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.sun.net.httpserver.*;
public class ThreadHttpServer {
//启动端口8080
+
private static final int port=8880;
private static final String Httpcontext="/demo";
private static final int nThreads=8;
public static void mains() {
+ System.out.println("S7链接");
+ S7control s7=new S7control(EPlcType.S1500, "192.168.30.14", 102, 0, 0);
+ System.out.println("S7:"+s7.readByte("DB34.0", 2));
HttpServer httpServer;
try {
httpServer=HttpServer.create(new InetSocketAddress(port),0);
diff --git a/nglib/src/ng/db/DBHelper.java b/nglib/src/ng/db/DBHelper.java
index fb645ad..80c3f9a 100644
--- a/nglib/src/ng/db/DBHelper.java
+++ b/nglib/src/ng/db/DBHelper.java
@@ -12,68 +12,69 @@
import java.util.Properties;
import org.json.*;
+import org.json.JSONArray;
public class DBHelper{
-
- private String sqlurl="jdbc:mysql://10.153.19.150/gmms";
+
+ private String sqlurl="jdbc:mysql://localhost/haier";
private String user="root";
private String password="beibo.123/";
static java.util.Hashtable<String, DBHelper> helper=new java.util.Hashtable<String, DBHelper>();
-
-
+
+
public static DBSession createDBSession(String sqlurl,String user,String password,boolean manulcomit) throws SQLException{
return DBSession.createSession(sqlurl, user, password,manulcomit);
}
-
+
public class ProcResult{
public int code;
public String Message;
- public JSONArray Datas;
+ public org.json.JSONArray Datas;
}
-
+
public static DBSession createDBSession(String name,boolean manulcomit) throws Exception{
if(helper.size()==0){
InitHelper();
}
- DBHelper ret= helper.get(name);
- if(ret!=null)
- return ret.createSession(manulcomit);
- throw new Exception("閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�");
+ DBHelper ret= helper.get(name);
+ if(ret!=null)
+ return ret.createSession(manulcomit);
+ throw new Exception("閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�");
}
-
-
-
+
+
+
public int update(String sql,Object...params) throws SQLException{
DBSession sn=null;
try{
- sn= createSession(false);
- sn.createSql(sql);
- return sn._update(params);
+ sn= createSession(false);
+ sn.createSql(sql);
+ return sn._update(params);
}
finally{
sn.close();
}
}
-
-
- public JSONArray query(boolean asObject,String sql,Object...params) throws SQLException, JSONException{
+
+
+ public org.json.JSONArray query(boolean asObject, String sql, Object...params) throws SQLException, JSONException{
DBSession sn=null;
try{
- sn= createSession(false);
- sn.createSql(sql);
- return sn._query(params).resultToJson(asObject);
+ sn= createSession(false);
+ sn.createSql(sql);
+ return sn._query(params).resultToJson(asObject);
}
finally{
sn.close();
}
}
-
-
-
-
+
+
+
+
public static DBHelper getDBHelper(String name){
return helper.get(name);
@@ -82,454 +83,454 @@
public static void addHelper(String name,String url,String user,String pass){
- DBHelper help=new DBHelper(url,user,pass);
- helper.put("mes",help);
+ DBHelper help=new DBHelper(url,user,pass);
+ helper.put("mes",help);
}
- public static void InitHelper() throws Exception{
+ public static void InitHelper() throws Exception{
- Properties properties = NGFunction.getProperties();
- Class.forName(properties.getProperty("driver"));
- String data= properties.getProperty("connection");
- if(data==null)
- {
- throw new Exception("鏈敓鎻鎷烽敓鏂ゆ嫹閿熸枻鎷�");
- }
- System.out.println(data);
- String[] names=data.split(",");
- for(int i=0;i<names.length;i++){
- DBHelper help=new DBHelper(properties.getProperty(names[i]+".url"),
- properties.getProperty(names[i]+".user"),
- properties.getProperty(names[i]+".password")
- );
- helper.put(names[i],help);
- System.out.println("create "+names[i]);
- }
-
- }
-
-
-
-
- public static void updateConnection(String name) throws Exception{
- Properties properties = NGFunction.getProperties();
- DBHelper ret= helper.get(name);
- if(ret==null){
- throw new Exception("閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�");
- }
- ret.sqlurl=properties.getProperty(name+".url");
- ret.user=properties.getProperty(name+".user");
- ret.password=properties.getProperty(name+".password");
- }
-
-
-
-
-
-
- public JSONArray query(String sql,Boolean RowAsObject,Object...params) throws SQLException, JSONException{
- DBSession sn=null;
- try{
- sn=this.createSession(false);
- sn.createSql(sql);
- return sn.query(params).resultToJson(RowAsObject);
-
- }
- finally{
- if(sn!=null)
- sn.close();
- }
+ Properties properties = NGFunction.getProperties();
+ Class.forName(properties.getProperty("driver"));
+ String data= properties.getProperty("connection");
+ if(data==null)
+ {
+ throw new Exception("鏈敓鎻鎷烽敓鏂ゆ嫹閿熸枻鎷�");
+ }
+ System.out.println(data);
+ String[] names=data.split(",");
+ for(int i=0;i<names.length;i++){
+ DBHelper help=new DBHelper(properties.getProperty(names[i]+".url"),
+ properties.getProperty(names[i]+".user"),
+ properties.getProperty(names[i]+".password")
+ );
+ helper.put(names[i],help);
+ System.out.println("create "+names[i]);
+ }
+
}
-
+
+
+
+
+ public static void updateConnection(String name) throws Exception{
+ Properties properties = NGFunction.getProperties();
+ DBHelper ret= helper.get(name);
+ if(ret==null){
+ throw new Exception("閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�");
+ }
+ ret.sqlurl=properties.getProperty(name+".url");
+ ret.user=properties.getProperty(name+".user");
+ ret.password=properties.getProperty(name+".password");
+ }
+
+
+
+
+
+
+ public org.json.JSONArray query(String sql, Boolean RowAsObject, Object...params) throws SQLException, JSONException{
+ DBSession sn=null;
+ try{
+ sn=this.createSession(false);
+ sn.createSql(sql);
+ return sn.query(params).resultToJson(RowAsObject);
+
+ }
+ finally{
+ if(sn!=null)
+ sn.close();
+ }
+ }
+
public int update(String sql,Boolean RowAsObject,Object...params) throws SQLException{
- DBSession sn=null;
- try{
- sn=this.createSession(false);
- sn.createSql(sql);
- return sn.update(params);
-
-
- }
- finally{
- if(sn!=null)
- sn.close();
- }
+ DBSession sn=null;
+ try{
+ sn=this.createSession(false);
+ sn.createSql(sql);
+ return sn.update(params);
+
+
+ }
+ finally{
+ if(sn!=null)
+ sn.close();
+ }
}
-
-
-
+
+
+
public void init(String sqlurl,String user,String password){
this.sqlurl=sqlurl;
this.user=user;
this.password=password;
}
-
-
+
+
private static boolean odbc_regist=false;
-
-
-
- //閿熸枻鎷烽敓鏂ゆ嫹涓�閿熸枻鎷烽敓鏂ゆ嫹in閿熸枻鎷烽敓鏂ゆ嫹 閿熸枻鎷烽敓鏂ゆ嫹out閿熸枻鎷烽敓鏂ゆ嫹閿熶茎瀛樺偍閿熸枻鎷烽敓鏁欙綇鎷蜂竴閿熸枻鎷烽敓琛楄鎷烽敓鏂ゆ嫹int 1閿熸枻鎷烽敓鏂ゆ嫹鏅掗敓锟� 涓�閿熸枻鎷烽敓鏂ゆ嫹String閿熸枻鎷蜂负涓�閿熸枻鎷烽敓鎴潻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓缁炑嶆嫹閿熺粸鎲嬫嫹閿熻娇顏庢嫹閿熺粸鎾呮嫹閿熻緝顫嫹閿燂拷
+
+
+
+ //閿熸枻鎷烽敓鏂ゆ嫹涓�閿熸枻鎷烽敓鏂ゆ嫹in閿熸枻鎷烽敓鏂ゆ嫹 閿熸枻鎷烽敓鏂ゆ嫹out閿熸枻鎷烽敓鏂ゆ嫹閿熶茎瀛樺偍閿熸枻鎷烽敓鏁欙綇鎷蜂竴閿熸枻鎷烽敓琛楄鎷烽敓鏂ゆ嫹int 1閿熸枻鎷烽敓鏂ゆ嫹鏅掗敓? 涓�閿熸枻鎷烽敓鏂ゆ嫹String閿熸枻鎷蜂负涓�閿熸枻鎷烽敓鎴潻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓缁炑嶆嫹閿熺粸鎲嬫嫹閿熻娇顏庢嫹閿熺粸鎾呮嫹閿熻緝顫嫹閿�?
public QueryResult SelectByProc(String name,String[] Params){
- Connection con=null;
- try{
- int length=Params.length;
-
- String s=String.format("{call %s(",name);
- for(int i=0;i<=length;i++){
- if(i==0)
- s+="?";
- else
- s+=",?";
- }
- s+=")}";
-
- con=DriverManager.getConnection(sqlurl,user,password);
- CallableStatement c=con.prepareCall(s);
- for(int i=0;i<length;i++){
- c.setString(i+1,Params[i]);
- }
- c.registerOutParameter(length+1, java.sql.Types.VARCHAR);
- ResultSet result= c.executeQuery();
- String rest=c.getString(length+1);
- QueryResult r=new QueryResult(con,c,result);
- if(rest!=null){
- if(rest.equals("success")){
- return r;
- }
+ Connection con=null;
+ try{
+ int length=Params.length;
+
+ String s=String.format("{call %s(",name);
+ for(int i=0;i<=length;i++){
+ if(i==0)
+ s+="?";
+ else
+ s+=",?";
+ }
+ s+=")}";
+
+ con=DriverManager.getConnection(sqlurl,user,password);
+ CallableStatement c=con.prepareCall(s);
+ for(int i=0;i<length;i++){
+ c.setString(i+1,Params[i]);
+ }
+ c.registerOutParameter(length+1, java.sql.Types.VARCHAR);
+ ResultSet result= c.executeQuery();
+ String rest=c.getString(length+1);
+ QueryResult r=new QueryResult(con,c,result);
+ if(rest!=null){
+ if(rest.equals("success")){
+ return r;
}
- r.Close();
- return null;
-
- }
- catch(Exception e){
- e.printStackTrace();
- if(con!=null)
+ }
+ r.Close();
+ return null;
+
+ }
+ catch(Exception e){
+ e.printStackTrace();
+ if(con!=null)
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
- return null;
- }
+ return null;
+ }
}
-
+
//閿熸枻鎷烽敓鏂ゆ嫹涓�閿熸枻鎷烽敓鏂ゆ嫹in閿熸枻鎷烽敓鏂ゆ嫹 涓�閿熸枻鎷穙ut閿熸枻鎷烽敓鏂ゆ嫹閿熶茎瀛樺偍閿熸枻鎷烽敓鏂ゆ嫹
public String InvokeSqlProc(String name,String[] Params){
- Connection con=null;
- try{
- int length=Params.length;
-
- String s=String.format("{call %s(",name);
- for(int i=0;i<=length;i++){
- if(i==0)
- s+="?";
- else
- s+=",?";
- }
- s+=")}";
-
- con=DriverManager.getConnection(sqlurl,user,password);
- CallableStatement c=con.prepareCall(s);
- for(int i=0;i<length;i++){
- c.setString(i+1,Params[i]);
- }
- c.registerOutParameter(length+1, java.sql.Types.VARCHAR);
- c.execute();
- String message=c.getString(length+1);
- c.close();
- con.close();
- return message;
- }
- catch(Exception e){
- e.printStackTrace();
- if(con!=null)
+ Connection con=null;
+ try{
+ int length=Params.length;
+
+ String s=String.format("{call %s(",name);
+ for(int i=0;i<=length;i++){
+ if(i==0)
+ s+="?";
+ else
+ s+=",?";
+ }
+ s+=")}";
+
+ con=DriverManager.getConnection(sqlurl,user,password);
+ CallableStatement c=con.prepareCall(s);
+ for(int i=0;i<length;i++){
+ c.setString(i+1,Params[i]);
+ }
+ c.registerOutParameter(length+1, java.sql.Types.VARCHAR);
+ c.execute();
+ String message=c.getString(length+1);
+ 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";
- }
+ return "fail";
+ }
}
-
-
+
+
//閿熸枻鎷烽敓鏂ゆ嫹涓�閿熸枻鎷烽敓鏂ゆ嫹in閿熸枻鎷烽敓鏂ゆ嫹 涓�閿熸枻鎷穙ut閿熸枻鎷烽敓鏂ゆ嫹閿熶茎瀛樺偍閿熸枻鎷烽敓鏂ゆ嫹
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)
+ 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";
- }
-
+ return "fail";
+ }
+
}
-
+
//閿熸枻鎷烽敓鏂ゆ嫹涓�閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹
public Connection getConnection() throws SQLException{
-
- return DriverManager.getConnection(sqlurl,user,password);
- //return DriverManager.getConnection(sqlurl,user,password);
+
+ return DriverManager.getConnection(sqlurl,user,password);
+ //return DriverManager.getConnection(sqlurl,user,password);
}
-
-
+
+
//閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鍖℃嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鎻紮鎷烽敓鏂ゆ嫹渚勯敓鏂ゆ嫹閿熻娇顎︾枻鈺痚lect 閿熸枻鎷烽�夐敓鏂ゆ嫹
public String MakeSelectOptionBySql(String sql,String field){
- QueryResult query=null;
- try{
- query=new QueryResult(sqlurl,user,password,sql);
- ResultSet result=query.Result;
+ 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()));
+ 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;
+ 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;
+ }
+ }
+
+
//閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鍖℃嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸彮浼欐嫹閿熻鐤ゎ煉鎷疯皨閿熸枻鎷穞body閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鎹凤綇鎷稰KName閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓琛楁锝忔嫹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]);
+ 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{
-
- Object o=result.getObject(idx);
- if(o!=null)
- sb.append(o);
- }
- sb.append("</td>");
+ else
+ fieldIndex[i]=result.findColumn(ShowField[i]);
}
- sb.append("</tr>\r\n");
+ //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();
}
- //sb.append("</tbody>\r\n");
- query.Close();
- return sb.toString();
- }
- catch(Exception e){
- if(query!=null)
- query.Close();
- e.printStackTrace();
- return null;
- }
+ 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;
}
-
+
//涓�閿熸枻鎷烽敓娲ュ崟鐨勮鎷疯閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰獟閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹涓�浜涢敓鏂ゆ嫹璇㈤敓鏂ゆ嫹婧愰敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹close缁熶竴閿熼叺鏀撅綇鎷烽敓鏂ゆ嫹閿熼叺鏀剧櫢鎷烽敓鏂ゆ嫹
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);
- }
-
+ private java.sql.Connection con;
+ private java.sql.Statement statement;
+ public java.sql.ResultSet Result;
- public QueryResult(Connection con,java.sql.Statement statement,ResultSet result){
- this.con=con;
- this.statement=statement;
- this.Result=result;
- }
-
-
- 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){
-
- }
- }
+ 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 QueryResult(Connection con,java.sql.Statement statement,ResultSet result){
+ this.con=con;
+ this.statement=statement;
+ this.Result=result;
+ }
+
+
+ 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){
+
+ }
+ }
+ }
+
public DBSession createSession(boolean ManulCommit) throws SQLException{
Connection con=this.getConnection();
return new DBSession(con,ManulCommit);
}
-
-
- public static org.json.JSONArray resultToJson(String[] fields,ResultSet result,boolean RowAsObject) throws SQLException, JSONException{
- JSONArray arr=new JSONArray();
-
- String[] cols=fields;
- if(RowAsObject){
- while(result.next()){
- JSONObject obj=new JSONObject();
- for(int i=0;i<cols.length;i++){
- Object o=result.getObject(i+1);
- if(o==null)
- o=JSONObject.NULL;
- obj.put(cols[i],o);
-
- }
- arr.put(obj);
- }
- }
- else{
- while(result.next()){
- JSONArray obj=new JSONArray();
- for(int i=0;i<cols.length;i++){
- Object o=result.getObject(i+1);
- if(o==null)
- o=JSONObject.NULL;
- obj.put(o);
- }
- arr.put(obj);
- }
- }
- return arr;
- }
-
-
-
-
- //閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鍖℃嫹閿熺獤顖ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鑿侀敓琛楁唻鎷烽敓闃额亷鎷烽敓杞跨嫬sonArray, 閿熸枻鎷烽敓绲塷wAsObject=true 姣忛敓鏂ゆ嫹閿熸枻鎷风洰閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷蜂负閿熸枻鎷烽敓鏂ゆ嫹閿熻璁规嫹閿熸枻鎷烽敓鏂ゆ嫹jsonobject 閿熸枻鎷烽敓鏂ゆ嫹姣忛敓鏂ゆ嫹閿熸枻鎷风洰閿熸枻鎷烽敓鏂ゆ嫹涓�閿熸枻鎷烽敓鏂ゆ嫹閿熻璁规嫹閿熸枻鎷烽敓鏂ゆ嫹 jsonarray閿熸枻鎷烽敓鏂ゆ嫹
- public static org.json.JSONArray resultToJson(ResultSet result,boolean RowAsObject) throws SQLException, JSONException{
- JSONArray arr=new JSONArray();
- ResultSetMetaData r= result.getMetaData();
- String[] cols=new String[r.getColumnCount()];
+ public static org.json.JSONArray resultToJson(String[] fields,ResultSet result,boolean RowAsObject) throws SQLException, JSONException{
+ org.json.JSONArray arr=new org.json.JSONArray();
- for(int i=0;i<cols.length;i++){
- cols[i]=r.getColumnLabel(i+1);
-
- }
- if(RowAsObject){
- while(result.next()){
- JSONObject obj=new JSONObject();
- for(int i=0;i<cols.length;i++){
- Object o=result.getObject(i+1);
- if(o==null)
- o=JSONObject.NULL;
- obj.put(cols[i],o);
- }
- arr.put(obj);
- }
- }
- else{
- while(result.next()){
- JSONArray obj=new JSONArray();
- for(int i=0;i<cols.length;i++){
- Object o=result.getObject(i+1);
- if(o==null)
- o=JSONObject.NULL;
- obj.put(o);
- }
- arr.put(obj);
- }
- }
- return arr;
- }
-
-
- public static void main(String[] ss) throws Exception{
+ String[] cols=fields;
+
+ if(RowAsObject){
+ while(result.next()){
+ JSONObject obj=new JSONObject();
+ for(int i=0;i<cols.length;i++){
+ Object o=result.getObject(i+1);
+ if(o==null)
+ o=JSONObject.NULL;
+ obj.put(cols[i],o);
+
+ }
+ arr.put(obj);
+ }
+ }
+ else{
+ while(result.next()){
+ org.json.JSONArray obj=new org.json.JSONArray();
+ for(int i=0;i<cols.length;i++){
+ Object o=result.getObject(i+1);
+ if(o==null)
+ o=JSONObject.NULL;
+ obj.put(o);
+ }
+ arr.put(obj);
+ }
+ }
+ return arr;
+ }
+
+
+
+
+
+ //閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎鍖℃嫹閿熺獤顖ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鑿侀敓琛楁唻鎷烽敓闃额亷鎷烽敓杞跨嫬sonArray, 閿熸枻鎷烽敓绲塷wAsObject=true 姣忛敓鏂ゆ嫹閿熸枻鎷风洰閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷蜂负閿熸枻鎷烽敓鏂ゆ嫹閿熻璁规嫹閿熸枻鎷烽敓鏂ゆ嫹jsonobject 閿熸枻鎷烽敓鏂ゆ嫹姣忛敓鏂ゆ嫹閿熸枻鎷风洰閿熸枻鎷烽敓鏂ゆ嫹涓�閿熸枻鎷烽敓鏂ゆ嫹閿熻璁规嫹閿熸枻鎷烽敓鏂ゆ嫹 jsonarray閿熸枻鎷烽敓鏂ゆ嫹
+ public static org.json.JSONArray resultToJson(ResultSet result,boolean RowAsObject) throws SQLException, JSONException{
+ org.json.JSONArray arr=new org.json.JSONArray();
+ ResultSetMetaData r= result.getMetaData();
+ String[] cols=new String[r.getColumnCount()];
+
+ for(int i=0;i<cols.length;i++){
+ cols[i]=r.getColumnLabel(i+1);
+
+ }
+ if(RowAsObject){
+ while(result.next()){
+ JSONObject obj=new JSONObject();
+ for(int i=0;i<cols.length;i++){
+ Object o=result.getObject(i+1);
+ if(o==null)
+ o=JSONObject.NULL;
+ obj.put(cols[i],o);
+ }
+ arr.put(obj);
+ }
+ }
+ else{
+ while(result.next()){
+ org.json.JSONArray obj=new JSONArray();
+ for(int i=0;i<cols.length;i++){
+ Object o=result.getObject(i+1);
+ if(o==null)
+ o=JSONObject.NULL;
+ obj.put(o);
+ }
+ arr.put(obj);
+ }
+ }
+ return arr;
+ }
+
+
+ public static void main(String[] ss) throws Exception{
// DBHelper.InitHelper();
// System.out.println(DBHelper.getDBHelper("mes").query(true,"select * from gmms_galss_task where width>? limit 1",100));
// System.out.println(DBHelper.getDBHelper("mes").query(true,"select * from gmms_galss_task where width>? limit 1",false));
// System.out.println(DBHelper.getDBHelper("mes").update("insert into abc (a,b,c) values (?,?,?)", 1,"2","3"));
- }
+ }
}
\ No newline at end of file
diff --git a/nglib/src/ng/db/DBSession.java b/nglib/src/ng/db/DBSession.java
index 932e03a..ee5af0b 100644
--- a/nglib/src/ng/db/DBSession.java
+++ b/nglib/src/ng/db/DBSession.java
@@ -2,360 +2,357 @@
import java.sql.*;
import java.util.*;
import org.json.*;
-
-
-
-
+import org.json.JSONArray;
public class DBSession implements AutoCloseable {
- private Connection con; //锟斤拷锟斤拷锟斤拷
- private boolean auto; //锟角凤拷为锟皆讹拷锟结交
- private Statement sql; //锟斤拷锟揭伙拷锟绞癸拷玫锟絊tatement
-
-
-
- public Connection getConnection(){
- return con;
- }
- //锟斤拷锟斤拷
- private void reset(){
- if(sql!=null){
- try{
- sql.close();
- }
- catch(Exception e){
-
- }
- sql=null;
- }
- }
-
-
-
-
- //锟斤拷询锟斤拷锟斤拷锟�
- public class SelectResult{
- private Statement state;
- private boolean closed;
-
-
- public Object getFirst() throws SQLException{
- ResultSet r= state.getResultSet();
- if(r.next())
- return r.getObject(1);
- else
- return null;
- }
- //锟斤拷取锟斤拷前锟侥斤拷锟�
- public ResultSet getCurrentResult() throws SQLException {
- return state.getResultSet();
- }
-
- public JSONObject allResultToJson() throws SQLException, JSONException{
- JSONObject obj=new JSONObject();
- int idx=1;
- do{
- JSONArray a=this.resultToJson(false);
- obj.put("Table"+idx,a);
- idx++;
- }while(this.nextResult());
- return obj;
- }
-
+ private Connection con; //锟斤拷锟斤拷锟斤拷
+ private boolean auto; //锟角凤拷为锟皆讹拷锟结交
+ private Statement sql; //锟斤拷锟揭伙拷锟绞癸拷玫锟絊tatement
-
- //锟狡讹拷锟斤拷锟斤拷一锟斤拷锟斤拷锟�,锟斤拷锟斤拷锟斤拷欧锟斤拷锟絫rue
- public boolean nextResult() throws SQLException {
- return state.getMoreResults();
- }
- public void close(){
- if(closed==false){
- closed=true;
- try{
- state.close();
- }
- catch(Exception e){
-
- }
- }
- }
-
- //锟斤拷锟届函锟斤拷
- public SelectResult(Statement state){
- this.state=state;
- ResultSet set=null;
- }
-
- //锟斤拷锟斤拷前锟侥斤拷锟斤拷锟街憋拷锟斤拷锟斤拷锟絁SON锟斤拷锟斤拷
- public JSONArray resultToJson(boolean RowAsObject) throws SQLException, JSONException{
- return DBHelper.resultToJson(this.getCurrentResult(), RowAsObject);
- }
-
- public JSONArray resultToJson(String[] fields,boolean RowAsObject) throws SQLException, JSONException{
- return DBHelper.resultToJson(fields,this.getCurrentResult(), RowAsObject);
- }
-
-
- public List<JSONArray> allToJson(boolean RowAsObject) throws SQLException, JSONException{
- List<JSONArray> arrs=new ArrayList<JSONArray>();
- do{
- arrs.add(DBHelper.resultToJson(this.getCurrentResult(), RowAsObject));
- }while(this.nextResult());
- return arrs;
- }
-
-
-
- }
-
- //锟斤拷准锟芥储锟斤拷锟教碉拷锟矫o拷锟斤拷准锟芥储锟斤拷锟教讹拷锟斤拷,锟斤拷锟侥碉拷
- public class StdCallResult extends SelectResult{
- public int ResultCode;
- public String ResultMessage;
- public StdCallResult(CallableStatement state,int ResultCode,String ResultMessage){
- super(state);
- this.ResultCode=ResultCode;
- this.ResultMessage=ResultMessage;
- }
- }
-
-
-
- //锟斤拷锟斤拷一锟斤拷SQL锟斤拷锟斤拷
- public void createSql(String sql) throws SQLException{
- reset();
- PreparedStatement result= con.prepareStatement(sql);
- this.sql=result;
- }
-
- public StdCallResult CallProc(String proc,Object... params) throws SQLException{
- createStdCall(proc,params.length);
- return this.stdCall(params);
- }
-
-
-
-
-
-
- public void createQueryCall(String procName,int InputParamCount) throws SQLException {
- reset();
- StringBuilder sp=new StringBuilder();
- sp.append("{call ");
- sp.append(procName);
- sp.append("(");
- for(int i=0;i<InputParamCount;i++){
- if(i==0){
- sp.append('?');
- }
- else{
- sp.append(",?");
- }
- }
-
- CallableStatement sql=con.prepareCall(sp.toString());
- sql.registerOutParameter(InputParamCount+1, java.sql.Types.INTEGER);
- sql.registerOutParameter(InputParamCount+2, java.sql.Types.VARCHAR);
- this.sql=sql;
- }
-
-
-
- //锟斤拷锟斤拷一锟斤拷锟斤拷准锟芥储锟斤拷锟教碉拷锟矫o拷锟斤拷锟斤拷1锟角存储锟斤拷锟教o拷锟斤拷锟斤拷2锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷母锟斤拷锟�
- public void createStdCall(String procName,int InputParamCount) throws SQLException {
- reset();
- StringBuilder sp=new StringBuilder();
- sp.append("{call ");
- sp.append(procName);
- sp.append("(");
- for(int i=0;i<InputParamCount;i++){
- if(i==0){
- sp.append('?');
- }
- else{
- sp.append(",?");
- }
- }
- if(InputParamCount==0)
- {
- sp.append("?,?)}");
- }
- else{
- sp.append(",?,?)}");
- }
- CallableStatement sql=con.prepareCall(sp.toString());
- sql.registerOutParameter(InputParamCount+1, java.sql.Types.INTEGER);
- sql.registerOutParameter(InputParamCount+2, java.sql.Types.VARCHAR);
- this.sql=sql;
- }
- //锟矫达拷锟斤拷锟斤拷SQL锟斤拷锟斤拷锟斤拷锟捷库,执锟斤拷INSERT DELETE UPDATE锟斤拷SQL锟斤拷洌� 锟斤拷锟斤拷锟斤拷?占位锟斤拷锟斤拷应锟侥憋拷锟斤拷
- public int update(Object... params) throws SQLException {
-
- PreparedStatement sql=(PreparedStatement)this.sql;
- if(params!=null){
-
- for(int i=0;i<params.length;i++){
- sql.setObject(i+1, params[i]);
- }
-
- }
- return sql.executeUpdate();
-
- }
-
- public int _update(Object[] params) throws SQLException {
-
- PreparedStatement sql=(PreparedStatement)this.sql;
- if(params!=null){
-
- for(int i=0;i<params.length;i++){
- sql.setObject(i+1, params[i]);
- }
-
- }
- return sql.executeUpdate();
-
-}
-
- public Object queryFirst(Object... params) throws SQLException{
- PreparedStatement sql=(PreparedStatement)this.sql;
- if(params!=null){
-
- for(int i=0;i<params.length;i++){
- sql.setObject(i+1, params[i]);
- }
-
- }
- if(sql.execute()){
- ResultSet r=new SelectResult(sql).getCurrentResult();
- if(r.next()){
- return r.getObject(1);
- }
- return null;
- }
- else
- return null;
- }
-
-
- //锟矫达拷锟斤拷锟斤拷SQL锟斤拷询锟斤拷锟捷库,执锟斤拷SELECT 锟斤拷洌� 锟斤拷锟斤拷锟斤拷?占位锟斤拷锟斤拷应锟侥憋拷锟斤拷
- public SelectResult query(Object... params) throws SQLException{
- PreparedStatement sql=(PreparedStatement)this.sql;
- if(params!=null){
-
- for(int i=0;i<params.length;i++){
- sql.setObject(i+1, params[i]);
- }
-
- }
- if(sql.execute()){
- return new SelectResult(sql);
- }
- else
- return null;
- }
-
-
-
-
- public SelectResult _query(Object[] params) throws SQLException{
- PreparedStatement sql=(PreparedStatement)this.sql;
- if(params!=null){
-
- for(int i=0;i<params.length;i++){
- sql.setObject(i+1, params[i]);
- }
-
- }
- if(sql.execute()){
- return new SelectResult(sql);
- }
- else
- return null;
- }
-
-
- //锟斤拷锟斤拷锟斤拷锟揭伙拷未锟斤拷锟斤拷牡谋锟阶硷拷娲拷锟斤拷蹋锟斤拷锟斤拷锟轿拷娲拷锟斤拷痰锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟角憋拷准锟斤拷锟矫斤拷锟斤拷锟斤拷锟斤拷锟芥储锟斤拷锟教达拷锟斤拷SELECT 锟斤拷锟皆碉拷锟斤拷SelectResult使锟斤拷
- public StdCallResult stdCall(Object...params) throws SQLException{
- CallableStatement sql=(CallableStatement)this.sql;
- if(params!=null){
- for(int i=0;i<params.length;i++){
- sql.setObject(i+1, params[i]);
- }
- }
- sql.execute();
- sql.getResultSet();
- int count= sql.getParameterMetaData().getParameterCount();
- int a1=sql.getInt(count-1);
- String a2=sql.getString(count);
- return new StdCallResult(sql,a1,a2);
- }
-
- //锟截闭会话
- public void close(){
- try{
- reset();
- if(con!=null){
- con.close();
- con=null;
- }
- }
- catch(Exception e){
-
- }
- }
-
- //锟街讹拷锟结交锟斤拷式锟铰o拷锟斤拷锟斤拷锟斤拷锟斤拷锟结交
- public void commit() throws SQLException{
- if(!auto){
- con.commit();
- }
- }
- //锟街讹拷锟结交锟斤拷式锟铰o拷锟斤拷锟节回癸拷锟斤拷锟斤拷
- public void rollback() throws SQLException{
- if(!auto){
- con.rollback();
- }
- }
-
- //锟结话锟侥癸拷锟届函锟斤拷
- public DBSession(Connection con,boolean ManulCommit) throws SQLException{
- con.setAutoCommit(!ManulCommit);
- this.auto=!ManulCommit;
- this.con=con;
- }
-
- public DBSession(Connection con) throws SQLException{
- this(con,false);
- }
-
-
- public static DBSession createSession(String url,String user,String password,boolean ManulCommit) throws SQLException{
- Connection con=null;
- DBSession sn=null;
- try{
- con= DriverManager.getConnection(url,user,password);
- sn=new DBSession(con,ManulCommit);
- return sn;
- }
- catch(SQLException e){
- if(sn!=null)
- sn.close();
- else
- {
- if(con!=null){
- con.close();
- }
- }
- throw e;
- }
- }
-
+ public Connection getConnection(){
+ return con;
+ }
+ //锟斤拷锟斤拷
+ private void reset(){
+ if(sql!=null){
+ try{
+ sql.close();
+ }
+ catch(Exception e){
-
-
-
+ }
+ sql=null;
+ }
+ }
+
+
+
+
+ //锟斤拷询锟斤拷锟斤拷锟�
+ public class SelectResult{
+ private Statement state;
+ private boolean closed;
+
+
+ public Object getFirst() throws SQLException{
+ ResultSet r= state.getResultSet();
+ if(r.next())
+ return r.getObject(1);
+ else
+ return null;
+ }
+ //锟斤拷取锟斤拷前锟侥斤拷锟�
+ public ResultSet getCurrentResult() throws SQLException {
+ return state.getResultSet();
+ }
+
+ public JSONObject allResultToJson() throws SQLException, JSONException{
+ JSONObject obj=new JSONObject();
+ int idx=1;
+ do{
+ org.json.JSONArray a=this.resultToJson(false);
+ obj.put("Table"+idx,a);
+ idx++;
+ }while(this.nextResult());
+ return obj;
+ }
+
+
+
+ //锟狡讹拷锟斤拷锟斤拷一锟斤拷锟斤拷锟�,锟斤拷锟斤拷锟斤拷欧锟斤拷锟絫rue
+ public boolean nextResult() throws SQLException {
+ return state.getMoreResults();
+ }
+ public void close(){
+ if(closed==false){
+ closed=true;
+ try{
+ state.close();
+ }
+ catch(Exception e){
+
+ }
+ }
+ }
+
+ //锟斤拷锟届函锟斤拷
+ public SelectResult(Statement state){
+ this.state=state;
+ ResultSet set=null;
+ }
+
+ //锟斤拷锟斤拷前锟侥斤拷锟斤拷锟街憋拷锟斤拷锟斤拷锟絁SON锟斤拷锟斤拷
+ public org.json.JSONArray resultToJson(boolean RowAsObject) throws SQLException, JSONException{
+ return DBHelper.resultToJson(this.getCurrentResult(), RowAsObject);
+ }
+
+ public org.json.JSONArray resultToJson(String[] fields, boolean RowAsObject) throws SQLException, JSONException{
+ return DBHelper.resultToJson(fields,this.getCurrentResult(), RowAsObject);
+ }
+
+
+ public List<org.json.JSONArray> allToJson(boolean RowAsObject) throws SQLException, JSONException{
+ List<org.json.JSONArray> arrs=new ArrayList<JSONArray>();
+ do{
+ arrs.add(DBHelper.resultToJson(this.getCurrentResult(), RowAsObject));
+ }while(this.nextResult());
+ return arrs;
+ }
+
+
+
+ }
+
+ //锟斤拷准锟芥储锟斤拷锟教碉拷锟矫o拷锟斤拷准锟芥储锟斤拷锟教讹拷锟斤拷,锟斤拷锟侥碉拷
+ public class StdCallResult extends SelectResult{
+ public int ResultCode;
+ public String ResultMessage;
+ public StdCallResult(CallableStatement state,int ResultCode,String ResultMessage){
+ super(state);
+ this.ResultCode=ResultCode;
+ this.ResultMessage=ResultMessage;
+ }
+ }
+
+
+
+ //锟斤拷锟斤拷一锟斤拷SQL锟斤拷锟斤拷
+ public void createSql(String sql) throws SQLException{
+ reset();
+ PreparedStatement result= con.prepareStatement(sql);
+ this.sql=result;
+ }
+
+ public StdCallResult CallProc(String proc,Object... params) throws SQLException{
+ createStdCall(proc,params.length);
+ return this.stdCall(params);
+ }
+
+
+
+
+
+
+ public void createQueryCall(String procName,int InputParamCount) throws SQLException {
+ reset();
+ StringBuilder sp=new StringBuilder();
+ sp.append("{call ");
+ sp.append(procName);
+ sp.append("(");
+ for(int i=0;i<InputParamCount;i++){
+ if(i==0){
+ sp.append('?');
+ }
+ else{
+ sp.append(",?");
+ }
+ }
+
+ CallableStatement sql=con.prepareCall(sp.toString());
+ sql.registerOutParameter(InputParamCount+1, java.sql.Types.INTEGER);
+ sql.registerOutParameter(InputParamCount+2, java.sql.Types.VARCHAR);
+ this.sql=sql;
+ }
+
+
+
+ //锟斤拷锟斤拷一锟斤拷锟斤拷准锟芥储锟斤拷锟教碉拷锟矫o拷锟斤拷锟斤拷1锟角存储锟斤拷锟教o拷锟斤拷锟斤拷2锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷母锟斤拷锟�
+ public void createStdCall(String procName,int InputParamCount) throws SQLException {
+ reset();
+ StringBuilder sp=new StringBuilder();
+ sp.append("{call ");
+ sp.append(procName);
+ sp.append("(");
+ for(int i=0;i<InputParamCount;i++){
+ if(i==0){
+ sp.append('?');
+ }
+ else{
+ sp.append(",?");
+ }
+ }
+ if(InputParamCount==0)
+ {
+ sp.append("?,?)}");
+ }
+ else{
+ sp.append(",?,?)}");
+ }
+ CallableStatement sql=con.prepareCall(sp.toString());
+ sql.registerOutParameter(InputParamCount+1, java.sql.Types.INTEGER);
+ sql.registerOutParameter(InputParamCount+2, java.sql.Types.VARCHAR);
+ this.sql=sql;
+ }
+
+ //锟矫达拷锟斤拷锟斤拷SQL锟斤拷锟斤拷锟斤拷锟捷库,执锟斤拷INSERT DELETE UPDATE锟斤拷SQL锟斤拷洌� 锟斤拷锟斤拷锟斤拷?占位锟斤拷锟斤拷应锟侥憋拷锟斤拷
+ public int update(Object... params) throws SQLException {
+
+ PreparedStatement sql=(PreparedStatement)this.sql;
+ if(params!=null){
+
+ for(int i=0;i<params.length;i++){
+ sql.setObject(i+1, params[i]);
+ }
+
+ }
+ return sql.executeUpdate();
+
+ }
+
+ public int _update(Object[] params) throws SQLException {
+
+ PreparedStatement sql=(PreparedStatement)this.sql;
+ if(params!=null){
+
+ for(int i=0;i<params.length;i++){
+ sql.setObject(i+1, params[i]);
+ }
+
+ }
+ return sql.executeUpdate();
+
+ }
+
+ public Object queryFirst(Object... params) throws SQLException{
+ PreparedStatement sql=(PreparedStatement)this.sql;
+ if(params!=null){
+
+ for(int i=0;i<params.length;i++){
+ sql.setObject(i+1, params[i]);
+ }
+
+ }
+ if(sql.execute()){
+ ResultSet r=new SelectResult(sql).getCurrentResult();
+ if(r.next()){
+ return r.getObject(1);
+ }
+ return null;
+ }
+ else
+ return null;
+ }
+
+
+ //锟矫达拷锟斤拷锟斤拷SQL锟斤拷询锟斤拷锟捷库,执锟斤拷SELECT 锟斤拷洌� 锟斤拷锟斤拷锟斤拷?占位锟斤拷锟斤拷应锟侥憋拷锟斤拷
+ public SelectResult query(Object... params) throws SQLException{
+ PreparedStatement sql=(PreparedStatement)this.sql;
+ if(params!=null){
+
+ for(int i=0;i<params.length;i++){
+ sql.setObject(i+1, params[i]);
+ }
+
+ }
+ if(sql.execute()){
+ return new SelectResult(sql);
+ }
+ else
+ return null;
+ }
+
+
+
+
+ public SelectResult _query(Object[] params) throws SQLException{
+ PreparedStatement sql=(PreparedStatement)this.sql;
+ if(params!=null){
+
+ for(int i=0;i<params.length;i++){
+ sql.setObject(i+1, params[i]);
+ }
+
+ }
+ if(sql.execute()){
+ return new SelectResult(sql);
+ }
+ else
+ return null;
+ }
+
+
+ //锟斤拷锟斤拷锟斤拷锟揭伙拷未锟斤拷锟斤拷牡谋锟阶硷拷娲拷锟斤拷蹋锟斤拷锟斤拷锟轿拷娲拷锟斤拷痰锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟角憋拷准锟斤拷锟矫斤拷锟斤拷锟斤拷锟斤拷锟芥储锟斤拷锟教达拷锟斤拷SELECT 锟斤拷锟皆碉拷锟斤拷SelectResult使锟斤拷
+ public StdCallResult stdCall(Object...params) throws SQLException{
+ CallableStatement sql=(CallableStatement)this.sql;
+ if(params!=null){
+ for(int i=0;i<params.length;i++){
+ sql.setObject(i+1, params[i]);
+ }
+ }
+ sql.execute();
+ sql.getResultSet();
+ int count= sql.getParameterMetaData().getParameterCount();
+ int a1=sql.getInt(count-1);
+ String a2=sql.getString(count);
+ return new StdCallResult(sql,a1,a2);
+ }
+
+ //锟截闭会话
+ public void close(){
+ try{
+ reset();
+ if(con!=null){
+ con.close();
+ con=null;
+ }
+ }
+ catch(Exception e){
+
+ }
+ }
+
+ //锟街讹拷锟结交锟斤拷式锟铰o拷锟斤拷锟斤拷锟斤拷锟斤拷锟结交
+ public void commit() throws SQLException{
+ if(!auto){
+ con.commit();
+ }
+ }
+ //锟街讹拷锟结交锟斤拷式锟铰o拷锟斤拷锟节回癸拷锟斤拷锟斤拷
+ public void rollback() throws SQLException{
+ if(!auto){
+ con.rollback();
+ }
+ }
+
+
+ //锟结话锟侥癸拷锟届函锟斤拷
+ public DBSession(Connection con,boolean ManulCommit) throws SQLException{
+ con.setAutoCommit(!ManulCommit);
+ this.auto=!ManulCommit;
+ this.con=con;
+ }
+
+ public DBSession(Connection con) throws SQLException{
+ this(con,false);
+ }
+
+
+ public static DBSession createSession(String url,String user,String password,boolean ManulCommit) throws SQLException{
+ Connection con=null;
+ DBSession sn=null;
+ try{
+ con= DriverManager.getConnection(url,user,password);
+ sn=new DBSession(con,ManulCommit);
+ return sn;
+ }
+ catch(SQLException e){
+ if(sn!=null)
+ sn.close();
+ else
+ {
+ if(con!=null){
+ con.close();
+ }
+ }
+ throw e;
+ }
+ }
+
+
+
+
+
}
--
Gitblit v1.8.0