JDBC连接数据库的方式

时间:2021-11-22 02:41:16

JDBC 是 Java 连接数据库的桥梁,可以说是非常重要的内容,那么它怎么连接到数据库也是非常基础非常重要的。

最近看视频学了点 JDBC,里面讲了三种方式连接数据库。

1、通过 Driver 连接

2、通过配置文件连接

3、通过 DriverManager 连接


一、用 Java 连接数据库,首先需要导入相应的 jar 包。

Oracle:找到 Oracle 的安装目录,找到 product 文件夹,里面有个 jdbc 文件夹,jar 包就在里面

mysql:去网上下载一个 jar 包,我这里的是 mysql-connector-java-5.1.13-bin.jar

打开 eclipse ,在相应的包中新建一个 lib 文件夹,把这两个 jar 包复制到该文件夹中,再右击它们 Build Path ---> Add to Build Path 即可

二、如果是 Oracle ,请确保启动了其相应的 服务器与监听器。右击我的电脑--管理--服务:如下图

JDBC连接数据库的方式

我这里还没启动。

三、写代码连接数据库吧

package jdbcConn;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import oracle.jdbc.driver.OracleDriver;

public class ConnectionTest {
public static void main(String[] args)
throws SQLException, IOException, Exception, IllegalAccessException, ClassNotFoundException {
ConnectionTest ct = new ConnectionTest();
//testDriver();
ct.getConnection();
//driverManagerTest();
}

private static void driverManagerTest()
throws SQLException, ClassNotFoundException {
//优势:a.简单方便。b.可以同时管理多个连接
String driverClass = "com.mysql.jdbc.Driver";
String driverClass2 = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverClass2);

String url = "jdbc:mysql://localhost:3306/test";
String url2 = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "root";
String user2 = "scott";
String password = "mysql";
String password2 = "tiger";
Connection conn = DriverManager.getConnection(url2, user2, password2);
System.out.println(conn);
}

private void getConnection()
throws IOException, InstantiationException, IllegalAccessException,
ClassNotFoundException, SQLException {
//该方式通过改变配置文件的内容来控制要连接的数据库
String driveClass = null;
String url = null;
String user = null;
String password = null;

InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driveClass = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");

Driver driver = (Driver) Class.forName(driveClass).newInstance();

Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(url, info);

System.out.println(connection);
}

private static void testDriver() throws SQLException {
//引入驱动
//Driver driver = new com.mysql.jdbc.Driver();
Driver driver2 = new OracleDriver();
//连接数据库地址
//String url = "jdbc:mysql://localhost:3306/test";
String url2 = "jdbc:oracle:thin:@localhost:1521:orcl";
Properties info = new Properties();
info.put("user", "scott");//输入相应的用户名
info.put("password", "tiger");//相应的密码
//连接
Connection connection = (Connection) driver2.connect(url2, info);
System.out.println(connection);
}
}


特别说明:

***Oracle 的连接形式:jdbc:oracle:thin:@localhost:1521:sid

***MySql 的连接形式:jdbc:mysql://localhost:3306/sid

***SQLServer 的连接形式:jdbc:microsoft:sqlserver//localhost:1433; DadabaseName = sid

记得把 sid 改成自己的数据库名

别导错包了

另附图:JDBC连接数据库的方式JDBC连接数据库的方式