kingbase数据库连接实例

1.首先需要数据库驱动  kingbasejdbc3.jar

2.数据库连接字符串 jdbc.properties

jdbc.connection.driver_class=com.kingbase.Driver
jdbc.connection.url=jdbc:kingbase://ip:端口号/数据库名称
jdbc.connection.username=数据库名称
jdbc.connection.password=数据库密码

3.连接jdbcutil 

package com.hyjx.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class JdbcUtils {
    

    
    public static Connection getConnection() throws Exception {
        String url = PropertiesUtil.getProperty("jdbc.connection.url");
        String driverClass = PropertiesUtil.getProperty("jdbc.connection.driver_class");
        String user = PropertiesUtil.getProperty("jdbc.connection.username");
        String password = PropertiesUtil.getProperty("jdbc.connection.password");
        Class.forName(driverClass);
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }
    
    public static void close(ResultSet rs,Statement st,Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(st != null){
            try {
                st.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    public static void rollback(Connection con) {
        try {
            if(con != null) {
                con.rollback();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 3.根据sql查询数据集合
     * */
    @SuppressWarnings("unchecked")
    public static List select(String sql) {
        Statement stat = null;
        ResultSet rs = null;
        ResultSetMetaData rsmd = null;
        java.sql.Connection conOrcale = null;
        List ll = new ArrayList();
        try {
            conOrcale = JdbcUtils.getConnection();
            stat = conOrcale.createStatement();
            // 得到结果集
            rs = stat.executeQuery(sql);
            // 用于获取关于 ResultSet 对象中列的类型和属性信息的对象
            rsmd = rs.getMetaData();
            while (rs.next()) {
                try {
                    Map rowData = new HashMap();
                    for (int i = 1; i <= rsmd.getColumnCount(); i++)
                        rowData.put(rsmd.getColumnName(i), rs.getString(i));
                        ll.add(rowData);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conOrcale.close();
                stat.close();
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ll;
    }
}
 

4.获取数据库配置文件中的工具类PropertiesUtil

package com.hyjx.common;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

public class PropertiesUtil {

    private static Properties prop = new Properties();
    
    static{
        try{
            InputStream is = PropertiesUtil.class.getResourceAsStream("/config/jdbc.properties");
            prop.load(is);
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    //private static String propName = null;
    
    /**
     * 装载指定的Properties文件
     * @param propName
     */
    public static void loadProperties(String propName) {
        String path = getPropertiesPath(propName);
        FileInputStream is = null;
        try {
            is = new FileInputStream(path);
            prop.load(is);
        } catch (Exception e) {
            System.out.println("没有找到" + path + "配置文件!");
        }finally {
            try {
                if(is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 获取Properties文件真实路径
     * @param propName
     * @return realPath
     */
    private static String getPropertiesPath(String propName) {
        String location = null;
        String realPath = null;
        try {
            location = URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("").getPath(),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        realPath = location;
        //System.out.println(location);
        if (location.lastIndexOf(".jar") != -1) {
            realPath = location.substring(0, location.lastIndexOf("/") + 1);
        }
        return realPath + propName;
    }
    
    /**
     * 根据key获取value
     * @param key
     * @return value
     */
    public static String getProperty(String key) {
        return prop.getProperty(key);
    }
}
 

猜你喜欢

转载自blog.csdn.net/kai402458953/article/details/89394362