JavaJDBC连接数据库Connection

Java DataBase Connectivity
Java数据库连接

写在前面

  • 注册驱动Class.forName()
  • 获取连接DriverManager.getConnection()

详细过程

  • mysql连接的url
String urlString = "jdbc:mysql://localhost:3306/数据库名";
  • 方式一
//创建mysql驱动对象
Driver driver = new com.mysql.jdbc.Driver();
//设置访问数据库的用户名和密码
Properties properties = new Properties();
properties.setProperty("user", "用户名");
properties.setProperty("password", "密码");
//建立连接
Connection connection = driver.connect(urlString, properties);
//关闭连接
connection.close();
  • 方式二
//创建mysql驱动对象
Driver driver = new com.mysql.jdbc.Driver();
//注册驱动
DriverManager.registerDriver(driver);
//通过DriverManager获取连接对象
Connection connection = DriverManager.getConnection(urlString, "用户名", "密码");
//关闭连接
connection.close();
  • 方式三
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
Connection connection = DriverManager.getConnection(urlString, "用户名", "密码");
//关闭连接
connection.close();

自定义的JdbcUtil类用来获取Connection对象

  • properties文件
url=jdbc:mysql://localhost:3306/javawu
user=root
password=root
driver=com.mysql.jdbc.Driver
  • JdbcUtil类
package com.javawu.util;
import java.io.FileInputStream;
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;
public class JdbcUtil {
    
    private static String driverString;
    private static String urlString;
    private static String userString;
    private static String passwordString;
    
    static {
        try {
            FileInputStream fileInputStream = new FileInputStream("./bin/db.properties");
            Properties properties = new Properties();
            properties.load(fileInputStream);
            urlString = properties.getProperty("url");
            driverString = properties.getProperty("driver");
            userString = properties.getProperty("user");
            passwordString = properties.getProperty("password");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    //获取连接对象
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driverString);
            connection = DriverManager.getConnection(urlString, userString, passwordString);
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
    
    //关闭相关对象、
    public static void close(Connection connection, Statement statement) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • JDBC简介 SUN公司为了简化、统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC。JDBC...
    奋斗的老王阅读 5,410评论 0 51
  • 实验目的 理解四种数据库(MySQL,HBase,Redis,MongoDB)的概念以及不同点。 熟练使用四种数据...
    Tiny_16阅读 18,066评论 5 12
  • 本人的环境为Myeclipse10、MySQL5.7.15 本文包括:简介JDBC编程步骤打通数据库程序详解—Dr...
    廖少少阅读 9,544评论 7 39
  • 世界上的所有石头在很久以前都是同一块,在一个雨夜,一声巨响 散落四方,谁也不曾想到过 有些石头去了高山,有些石头沉...
    木子火山阅读 1,768评论 0 0
  • 这几天事情很多,各种各样的事情涌来;虽然不是什么大事情,但遇到本来情绪就比较低落的自己,心情就更糟起来,睡眠也不够...
    黑白键弹不出的旋律阅读 1,553评论 2 1