All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docshare.orm.mysql.MySQLDBHelper Maven / Gradle / Ivy

Go to download

An efficient, fast, convenient, easy to learn, easy to use MVC framework and ORM framework. It is seamless compatible with JSTL and supports FreeMarker. It can run independently, and it can also be applied to traditional Java Web projects. It is an efficient, fast, convenient, easy to learn and easy to use MVC framework and ORM framework. It is seamless compatible with JSTL and supports FreeMarker. It can be run on its own, or it can be applied to traditional Java Web projects

There is a newer version: 2023.06.19
Show newest version
package org.docshare.orm.mysql;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.docshare.log.Log;
import org.docshare.mvc.Config;
import org.docshare.mvc.except.MVCException;
import org.docshare.orm.ColumnDesc;
import org.docshare.orm.DBHelper;
import org.docshare.orm.StatementPool;
import org.docshare.util.FileTool;
import org.docshare.util.TextTool;

import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;

public class MySQLDBHelper extends DBHelper {

	//防止用户自己调用构造函数
	public MySQLDBHelper(){
		
	}
	@Override
	public void conn() {
		try {
			if(con !=null && ! con.isClosed() && con.isValid(5))return;
			statementPool.clear();//清空语句池
			
			Log.d("MySQLDBHelper connect to server");
			Class.forName("com.mysql.jdbc.Driver");
			//Class.forName("com.mysql.cj.jdbc.Driver");
//			String uri = String.format(
//					"jdbc:mysql://%s:%s/%s?characterEncoding=utf-8&useSSL="+Config.useSSL,
//					Config.dbhost,Config.dbport, Config.dbname);
			String encoding = Config.dbEncoding;
			if(encoding.contains("utf8")) encoding = "utf8";
			String uri = TextTool.concat("jdbc:mysql://",Config.dbhost,":",Config.dbport,"/",Config.dbname,"?characterEncoding=",encoding,"&useSSL=",Config.useSSL,"&serverTimezone=Asia/Shanghai").toString();
//			String uri = String.format(
//					"jdbc:mysql://%s:%s/%s?characterEncoding=utf-8&serverTimezone=GMT+8&useSSL="+Config.useSSL,
//					Config.dbhost,Config.dbport, Config.dbname);
			con = DriverManager.getConnection(uri, Config.dbusr, Config.dbpwd);
			update("set names "+Config.dbEncoding);
		} catch (ClassNotFoundException e) {
			Log.e(e);
		} catch (SQLException e) {
			Log.e(e);
			Log.e(Config.str());
		}
	}

	public static void main(String[] args) throws SQLException {
		Config.dbport="3308";
		Config.dbname="mvc_demo";
		DBHelper helper = DBHelper.getIns();
		helper.conn();
		// dh.listColumn("book");
		System.out.println(helper.keyColumn("book"));
		
		ResultSet rs = helper.getRS("select * from book");
		while(rs.next()){
			String s = rs.getString("name");
			System.out.println(s);
		}
		
		HashMap cc = helper.columeOfRs("select * from book", rs);
		System.out.println(JSON.toJSONString(cc));
		
		cc = helper.listColumn("book");
		System.out.println(JSON.toJSONString(cc));
		
		String key = helper.keyColumn("book");
		System.out.println(key);
		
		cc = helper.listColumn("catalog");
		System.out.println(JSON.toJSONString(cc));
		rs.close();
		
	}
	/**
	 * 执行sql语句。 该函数会发现连接断开错误并自动重试3次,如果重试三次都无法成功则抛出SQLException
	 */
	public ResultSet getRS(String sql, List params) throws SQLException {
		printParams(sql,params);
		PreparedStatement s ;
		if(con==null || con.isClosed())conn();
		int retry =0;
		while(retry<3){
			try {
				s= statementPool.get(con, sql);   //con.prepareStatement(sql);
				for(int i=0;i© 2015 - 2024 Weber Informatics LLC | Privacy Policy