JDBC mysql 问题

与oracle不同是 在加载驱动的时候要用
Class.forName(driverName).newInstance();
其中的driverName用
driverName = "com.mysql.jdbc.Driver";
获取链接的时候用
Connection conn = DriverManager.getConnection(url);
其中的url用
private static String url = "jdbc:mysql://ip地址:端口/数据库名称?user=用户名&password=密码&useUnicode=true&characterEncoding=utf-8";

JDBC工具类举例:

package util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/*
 * JdbcUtil工具类
 * 封装jdbc中重复的内容
 * 1.加载驱动
 * 2.创建连接
 * 3.关闭资源
 */
public class JdbcUtil {
    private static String driverName=null;
    private static String userName=null;
    private static String password=null;
    private static String url=null;
    private JdbcUtil(){}
    
    
    static {
        try {
            Properties prop=new Properties();
            prop.load(JdbcUtil.class.getClassLoader().
                    getResourceAsStream("db.properties"));
            driverName=prop.getProperty("driverName");
            url=prop.getProperty("url");
            userName=prop.getProperty("userName");
            password=prop.getProperty("password");
            //加载驱动
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
            
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    //建立连接
    public static  Connection getConnection() throws SQLException{
        Connection conn=DriverManager.
                   getConnection(url,userName,password);
        return conn;
    }
    
    //关闭连接
    public static void close(ResultSet rs, Statement stmt,Connection conn){
        
            try {
                if(rs!=null){
                rs.close();
                }
            } catch (SQLException e) {
               e.printStackTrace();
               throw new RuntimeException(e);
            }finally{
                
                    try {
                        if(stmt!=null){
                        stmt.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }finally{
                        try {
                            if(conn!=null){
                             conn.close();
                            }
                        } catch (SQLException e) {
                            e.printStackTrace();
                            throw new RuntimeException(e);
                        }
                    }
            }
    }
    
    public static void main(String[] args) throws SQLException {
        Connection conn=JdbcUtil.getConnection();
        System.out.println(conn);
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本人的环境为Myeclipse10、MySQL5.7.15 本文包括:简介JDBC编程步骤打通数据库程序详解—Dr...
    廖少少阅读 4,391评论 7 39
  • JDBC简介 SUN公司为了简化、统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC。JDBC...
    奋斗的老王阅读 1,672评论 0 51
  • 一、相关概念 1.什么是JDBC JDBC(Java Data Base Connectivity,java数据库...
    ZZS_简阅读 388评论 0 0
  • 尤丽珍回到家中,尤丽珍的妈妈显得很是着急,看到尤丽珍就这样悄无声息的回到家里,急忙的问道:“女儿呀,你怎么样?...
    墨文沥阅读 403评论 0 0
  • 一切的蓄势待发都只为成就今夜的圆梦百次,一切的无言默契都只为打造今夜的头马传承。转身望去,每一次活动的样子我们历历...
    冷瓷阅读 646评论 0 1

友情链接更多精彩内容