JDBC connection MySQL, SQLServer, Oracle three kinds of database

MySQL JDBC database connection

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCTest {
    public static void main(String[] args) {
        String driverName="com.mysql.cj.jdbc.Driver";
        String dbURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        // 你的MySQL登录名,自己写,比如root
        String userName="userName";
        // 你的MySQL登录密码,自己写
        String userPassword="userPassword";
        try{
            Class.forName(driverName);
            System.out.println("加载MySQL驱动成功");
        } catch(ClassNotFoundException e) {
            System.err.println("加载MySQL驱动失败");
        }
        try (Connection dbConnection = DriverManager.getConnection(dbURL, userName, userPassword)) {
            System.out.println("连接数据库成功");
        } catch(SQLException e) {
            System.err.println("数据库连接失败");
        }
    }
}

SQLServer JDBC database connection

import java.sql.*;
 
public class Main{
    public static void main(String[] args) {
        String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
        // database是数据库名称,要自己填写
        String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=database";
        // 你的SQLServer登录名,自己写
        String userName="userName";
        // 你的SQLServer登录密码,自己写
        String userPassword="userPassword";
        try{
            Class.forName(driverName);
            System.out.println("加载SQLServer驱动成功");
        } catch(ClassNotFoundException e) {
            System.err.println("加载SQLServer驱动失败");
        }
        try (Connection dbConnection = DriverManager.getConnection(dbURL, userName, userPassword)) {
            System.out.println("连接数据库成功");
        } catch(SQLException e) {
            System.err.println("数据库连接失败");
        }
    }
}

Oracle Database JDBC connection

import java.sql.*;

public class Main{
    public static void main(String[] args) {
        String driverName="oracle.jdbc.driver.OracleDriver";
        // databaseName是数据库名称,要自己填写
        String dbURL="jdbc:oracle:thin:@localhost:1521:databaseName";
        // 你的Oracle登录名,自己写
        String userName="userName";
        // 你的Oracle密码,自己写
        String userPwd="userPassword";
        try{
            Class.forName(driverName);
            System.out.println("加载Oracle驱动成功");
        } catch(ClassNotFoundException e) {
            System.err.println("加载Oracle驱动失败");
        }
        try (Connection dbConnection = DriverManager.getConnection(dbURL, userName, userPassword)) {
            System.out.println("连接数据库成功");
        } catch(SQLException e) {
            System.err.println("数据库连接失败");
        }
    }
}
Published 652 original articles · won praise 1340 · Views 580,000 +

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104707654