資料庫連線URL及Drive(JAVA)
阿新 • • 發佈:2019-02-02
1. MongoDB
URL:
Driver:
2. MySql
URL: jdbc:mysql://localhost:3306/{dbname}
Driver: com.mysql.jdbc.Driver
3. PostgrelSql
URL: jdbc:postgresql://localhost:5432/{dbname}
Driver: org.postgresql.Driver
4. Redis
URL:
Driver:
5. Oracle
URL: jdbc:oracle:thin:@localhost:1521:{dbname}
Driver: oracle.jdbc.driver.OracleDriver
資料庫連線範例:
static Connection conn = null; static String DBServer = "localhost"; //資料庫伺服器名稱或IP static String DBName = "test"; //資料庫名稱 static String DBUser = "root"; //資料庫使用者 static String DBPassword = "yourpassword"; //資料庫密碼 public static Connection getConnection() { try { //載入jdbc驅動 Class.forName("com.mysql.jdbc.Driver"); System.out.println("Success loading Mysql Driver!"); //資料庫連線語句 String url = "jdbc:mysql://" + DBServer + ":3306/"+ DBName; conn = DriverManager.getConnection(url,DBUser,DBPassword); if (conn != null) System.out.println("資料庫連線成功!"); } catch(Exception e) { System.out.println("無法連線資料庫!"); e.printStackTrace(); } return conn; }