第一种方式:通过数据库本身的JDBC Driver连接到数据库
Classs.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.33:1521:huihoo","scott","tiger");
第二种方式:通过JDBC-ODBC桥连接到数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:192.168.1.33","scott","tiger");
192.168.1.33为数据源
完整的用户登录
Properties props = new Properties();
props.put("user", "scott");
props.put("password", "tiger");
Driver myDriver = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = myDriver.connect("jdbc:oracle:thin:@192.168.1.33:1521:huihoo", props);
conn.close();
System.out.println("成功登录.");
System.out.println("欢迎您 "+props.getProperty("user")+"!");