JDBC链接各种数据库的例子 / Java jdbc example holder

java语言下 jdbc 各种数据库的链接方式 mysql/mariadb 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 private static Connection getConn() { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/samp_db"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } oracle (1)Oracle JDBC Thin using a ServiceName: 1 2 #jdbc:oracle:thin:@//<host>:<port>/<service_name> #Example: jdbc:oracle:thin:@//192.168.2.1:1521/XE (2)Oracle JDBC Thin using an SID: 1 2 #jdbc:oracle:thin:@<host>:<port>:<SID> #Example: jdbc:oracle:thin:@192.168.2.1:1521:X01A (3)Oracle JDBC Thin using a TNSName: 1 2 #jdbc:oracle:thin:@<TNSName> #Example: jdbc:oracle:thin:@GL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // 获得连接对象 private static Connection getConn() { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@//127.0.0.1:1521/orcl"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } sqlserver 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // sqlserver2012 private static Connection getConn() { String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url = "jdbc:sqlserver://localhost:1433;DatabaseName=student"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } db2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 private static Connection getConn() { String driver = "com.ibm.db2.jcc.DB2Driver"; String url = "jdbc:db2://10.27.70.33:60000/dbtest:currentSchema=db2inst1"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } PostgreSQL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 private static Connection getConn() { String driver = "org.postgresql.Driver"; String url = "jdbc:postgresql://127.0.0.1:5432/test"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } greenplum 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 private static Connection getConn() { String driver = "com.pivotal.jdbc.GreenplumDriver"; String url = "jdbc:pivotal:greenplum://10.10.10.10:5432;DatabaseName=mfg"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } hana 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 private static Connection getConn() { String driver = "com.sap.db.jdbc.Driver"; String url = "jdbc:sap://172.23.1.123:39015"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } hive 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 private static Connection getConn() { /** hiveserver org.apache.Hadoop.hive.jdbc.HiveDriver jdbc:hive://localhost:10000/default hiveserver2 org.apache.Hive.jdbc.HiveDriver jdbc:hive2://localhos:10000/default */ String driver = "org.apache.hive.jdbc.HiveDriver"; String url = "jdbc:hive2://localhos:10000/default"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } sybase 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 private static Connection getConn() { String Driver = "com.sybase.jdbc3.jdbc.SybDriver"; //数据库驱动 String url = "jdbc:sybase:Tds:192.168.2.103:5000/SXABC"; // 连接的数据库是SXABC String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }

September 13, 2019 · 4 min · 682 words · atovk