有时候需要用jdbc测试数据库的连接性,但是总是记不起来怎么写。 这里还是总结一下,以备后用。
Mysql
String driver = "com.mysql.jdbc.Driver";// 驱动程序名 String dbname = "mydb"; String ip = "192.168.56.50"; String port = "3306"; String username = "root"; String password = "root"; String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbname; // 加载驱动程序 Class.forName(driver); // 连接数据库 Connection conn = DriverManager.getConnection(url, username, password);
Oracle
String driver = "oracle.jdbc.driver.OracleDriver"; // 驱动程序名 String dbname = "mydb"; String ip = "192.168.56.50"; String port = "1521"; String user = "root"; String password = "root"; String url = "jdbc:oracle:thin:@" + ip + ":" + port + ":" +dbname; //加载数据库驱动 Class.forName(driver); //连接数据库 Connection conn = DriverManager.getConnection(url, user, password);