SoFunction
Updated on 2025-03-08

Analysis of MySql public method instance based on JDBC access

Originally, the project used tools such as Hibernate, but because the project now requires that it is now back to the JDK-based solution.

This method is very simple, but for data connections, the connection pool is also directly initialized.

package com;
import .*;
import .*;
import ;
/**
  * @Description Database connection management
  * @author cuisuqiang
  */
public class ConnectionManager {
	/**
	  * @Description Execute a SQL
	  */
	@SuppressWarnings("unchecked")
	public static List<Object[]> excuteQuery(String sql) {
		Connection conn = null;
		PreparedStatement psta = null;
		ResultSet resultSet = null;
		List<Object[]> relist = new ArrayList<Object[]>(); // Total data		Object[] objects = null; // Each row of data		try {
			conn = (); // Get the link			if(null != conn){
				psta = (sql);
				resultSet = (); // Execute the query and return the result collection				int count = ().getColumnCount(); // How many columns of data are there in total				// Circular row				while (()) {
					objects = new Object[count];
					// The dataset index starts at 1, while the array is stored at 0					for (int i = 1; i <= count; i++) {
						objects[i - 1] = (i);
					}
					(objects);
				}
			}
		} catch (Exception e) {
			();
			relist = null;
		} finally {
			try {
				if(null != resultSet)
					();
				if(null != psta)
					();
				if(null != conn)
					();
			} catch (Exception e2) {
				
			}
		}
		return relist;
	}
	private static  ds = null;
	static {
		ds = new BasicDataSource(); // Create a data source object		int initialSize = 1; // Initial value when the connection pool starts		int maxActive = 10; // Maximum value of connection pool		int maxIdle = 1;   // Maximum idle value		int minIdle = 1;   // Minimum idle value		("");
		("jdbc:mysql://192.168.154.128:3306/t2?useUnicode=true&characterEncoding=gbk");
		("root");
		("123456");
		(initialSize);
		(maxActive);
		(maxIdle);
		(minIdle);
	}
	/**
	  * Get database connection from the data source
	  */
	public static Connection getConn() {
		try {
			return ();
		} catch (SQLException e) {
			();
		}
		return null;
	}
}

I am using BasicDataSource, you may need two Jar packages, and if you connect to mysql, you need mysql-connector-java-3.1.

Write a mian method to test it directly:

package com; 
import ; 
public class T { 
  @SuppressWarnings("unchecked") 
  public static void main(String[] args) { 
    try { 
      List<Object[]> list = ("select * from t"); 
      for (int i = 0; i < (); i++) { 
        Object[] os = (i); 
        for(Object o : os){ 
          if (o instanceof String) { 
            String s = (String) o; 
            String newStr = new String(("ISO-8859-1"),"GBK"); 
            ("String:" + newStr + "\t\t"); 
          }else if(o instanceof Long){ 
            Long s = (Long) o; 
            ("Floating point value:" + s + "\t\t"); 
          }else if(o instanceof Integer){ 
            Integer s = (Integer) o; 
            ("Surgery value:" + s + "\t\t"); 
          }else{ 
            ("Unknown type:" + o + "\t\t"); 
          } 
        } 
        (); 
      } 
    } catch (Exception e) { 
      (); 
    }     
  } 
}

What I'm returning is a List collection containing an Object array. After receiving this return set, the caller can parse according to the actual situation. The public method simply executes SQL and then gets a data connection for data access.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.