天天看點

工具類 驗證資料庫連通性 - 驗證SQL執行是否成功

/**
 * 1、驗證資料庫連通性
 * 2、驗證SQL執行是否成功
 * @date 2021/2/1 19:05
 */

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JDBCTool {
    public static Connection getConnection(String driver,String url,String name,String pwd){

        try{
            Class.forName(driver);
            Connection conn=DriverManager.getConnection(url,name,pwd);//擷取連接配接對象
            return conn;
        }catch(ClassNotFoundException e){
            System.out.println("ClassNotFoundException!");
            //e.printStackTrace();
            return null;
        }catch(SQLException e){
            //e.printStackTrace();
            return null;
        }
    }
    public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
        }catch(SQLException e){
           // e.printStackTrace();
        }
        try{
            if(ps!=null){
                ps.close();
            }
        }catch(SQLException e){
            //e.printStackTrace();
        }
        try{
            if(conn!=null){
                conn.close();
            }
        }catch(SQLException e){
          //  e.printStackTrace();
        }
    }

    public static Map<String,Object> checkConn(String driver, String url, String name, String pwd){

        Map<String,Object> rs =new HashMap();
        Connection cc=JDBCUtilTool.getConnection(driver,url,name,pwd);
        try {
            if(cc!=null){
                if(!cc.isClosed()){
                    rs.put("resultCode",0);
                    rs.put("resultMsg","資料庫連接配接成功!");
                }
            }else{
                rs.put("resultCode",-1);
                rs.put("resultMsg","資料庫連接配接失敗!");
            }
        } catch (SQLException e) {
            rs.put("resultCode",-1);
            rs.put("resultMsg",e);
        }
        return rs;
    }


    private static List convertList(ResultSet rs) throws SQLException{
        List list = new ArrayList();
        ResultSetMetaData md = rs.getMetaData();//擷取鍵名
        int columnCount = md.getColumnCount();//擷取行的數量
        while (rs.next()) {
            Map rowData = new HashMap();//聲明Map
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));//擷取鍵名及值
            }
            list.add(rowData);
        }
        return list;
    }

    public static Map<String,Object> checkSql(String driver, String url, String name, String pwd,String sql){

        Map<String,Object> rs =new HashMap();
        Connection cc=JDBCUtilTool.getConnection(driver,url,name,pwd);
        try {
            if(cc!=null){
                Statement statement = cc.createStatement();
                ResultSet rsValue = statement.executeQuery(sql);
                rs.put("resultCode",0);
                rs.put("resultMsg",convertList(rsValue).toString());
                //System.out.println("查詢出來資料"+convertList(rsValue).toString());
            }else{
                rs.put("resultCode",-1);
                rs.put("resultMsg","資料庫連接配接失敗!");
            }

        } catch (SQLException e) {
            rs.put("resultCode",-1);
            rs.put("resultMsg",e);
        }
        return rs;
    }

    public static void main(String[] args)
    {

        String driver="com.mysql.jdbc.Driver";           
        String url="jdbc:mysql://192.168.2.196:3307/mydb"; 
        String name="121232";                              
        String pwd="1312313";                            
        String sql="select * from tp_busi_attr";

        Map cc=JDBCUtilTool.checkConn(driver,url,name,pwd);

        System.out.println(cc.toString());

        Map ss=JDBCUtilTool.checkSql(driver,url,name,pwd,sql);

      
        System.out.println(ss.toString());
    }
}