从小伙伴那里拿来的源码
从数据库中读取数据然后存入哈希表中,然后再进行查删增改各种操作,再转为json数据与前段进行交互
package com.gxut.msqlutil;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import com.gxut.util.MyX509TrustManager;
import com.mysql.jdbc.Driver;
import com.mysql.jdbc.ResultSetMetaData;
public class Tools { //工具类
public static Connection connect() { //建立数据库连接
Connection con=null;
Driver.getOSName();
String userName = "root", password = "root";
String url = "jdbc:mysql://localhost:3306/wx003";
try {
if(con==null){
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection(url, userName, password);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return con;
}
public static ArrayList query(String sql) { //查询数据库函数
Connection con = connect();
if(con==null){
return null;
}
ArrayList al = new ArrayList();
try {
PreparedStatement ps= (PreparedStatement) con.prepareStatement(sql);
ResultSet rs= ps.executeQuery();
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
int colcount = rsmd.getColumnCount();//取得全部列数
while (rs.next()) {
HashMap hm = new HashMap();
for (int i = 0; i < colcount; i++) {
hm.put(i + 1, rs.getObject(i + 1));
} //END for(int i=...)
al.add(hm);
}
rs.close();
ps.close();
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return al;
}
public static void changeData(String sql) { //插入、修改或者删除数据库数据
Connection con = connect();
try {
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
int result = ps.executeUpdate();
if (result > 0) {
System.out.println("操作成功!");
}
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static String code(String s) { //解决浏览器中文乱码函数
String str = s;
try {
byte b[] = str.getBytes("ISO-8859-1");
str = new String(b);
return str;
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
public static SSLSocketFactory createTrustManager(){ //创建信任证书
//创建自己受信任的证书
MyX509TrustManager[] tm = { new MyX509TrustManager() };
SSLSocketFactory ssf=null;
try {
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
ssf = sslContext.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return ssf;
}
public static HttpsURLConnection getHttps(URL url,String requestMethod){ //返回安全https
HttpsURLConnection https = null;
try {
https = (HttpsURLConnection) url.openConnection();
https.setSSLSocketFactory(Tools.createTrustManager()); //设置安全证书
https.setRequestMethod(requestMethod); //必须是get方式请求
https.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
https.setDoOutput(true);
https.setDoInput(true);
https.setUseCaches(false);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
https.connect();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return https;
}
public static HttpURLConnection getHttp(URL url,String requestMethod){ //返回http
HttpURLConnection http=null;
try{
http= (HttpURLConnection) url.openConnection();
http.setRequestMethod(requestMethod);
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
http.setUseCaches(false);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
http.connect();
}
catch(Exception e){
e.printStackTrace();
}
return http;
}
/**
* utf 编码
*
* @param source
* @return
*/
public static String urlEncodeUTF8(String source) {
String result = source;
try {
result = java.net.URLEncoder.encode(source, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
}