import java.sql.*;
public class DBConnectTest { private static Connection conn;
public static void main(String[] args) { // 测试连接Oracle817数据库 testOracleConnect(); // 测试连接Mssql2000数据库 // testMSSQLConnect(); } static void testOracleConnect() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); String lUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; conn = DriverManager.getConnection(lUrl, "scott", "tiger"); String sSQL = "select empno,ename from EMP"; Statement stmt = conn.createStatement(); ResultSet rsResult = stmt.executeQuery(sSQL); while (rsResult.next()) { System.out.println(rsResult.getString(2)); } rsResult.close(); stmt.close(); conn.close(); } catch (Exception e) { System.out.println("Not Found:" + e.getMessage()); } } static void testMSSQLConnect() { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); String lUrl = "jdbc:microsoft:sqlserver://127.0.0.1:1433"; conn = DriverManager.getConnection(lUrl, "sa", "pwd"); String sSQL = "select people_name from hr_people"; Statement stmt = conn.createStatement(); ResultSet rsResult = stmt.executeQuery(sSQL); while (rsResult.next()) { System.out.println(rsResult.getString(1)); } rsResult.close(); stmt.close(); conn.close(); } catch (Exception e) { System.out.println("Not Found:" + e.getMessage()); } } }
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>