JDBC连接数据库操作
1.连接orcale数据库
-
驱动:oracle.jdbc.driver.OracleDriver
-
url=jdbc:oracle:thin:@ip:端口号:数据库名
public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
//JDBC对Oracle的url标准:jdbc:oracle:thin:@ip:端口号:数据库名
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
String username = "scott";
String password = "tiger";
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println(connection);
}
2.连接mysql数据库(5.0版本)
-
驱动:com.mysql.jdbc.Driver
-
url:jdbc:mysql://ip:端口号/数据库名
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
//mysql8.0版本连接数据库时需要在url中手动设置serverTimezone=UTC;
String url="jdbc:mysql://127.0.0.1:3306/test";
String username="root";
String password = "root";
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println(connection);
}
3.连接mysql数据库(8.0版本)
-
驱动:com.mysql.cj.jdbc.Driver
-
url:jdbc:mysql://ip:端口号/数据库名
url8.0标准写法:jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
注意:mysql8.0版本连接数据库时需要在url中手动设置serverTimezone=UTC;
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
//mysql8.0版本连接数据库时需要在url中手动设置serverTimezone=UTC;
String url="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC";
String username="root";
String password = "root";
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println(connection);
}