日期:2014-05-16  浏览次数:20371 次

Java 连接 Oracle
1. 建立驱动
2. 连接数据库
3. 发送sql语句
4. 关闭连接

    
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@192.168.209.82:1521:mySID";
public static final String DBUSER = "scott";
public static final String DBPASS = "tiger";


try {
    	  // 1. 建立驱动
    	    Class.forName (DBDRIVER);
    	  
    	  // 2. 连接数据库 
    	    conn = DriverManager.getConnection (DBURL, DBUSER, DBPASS);
    	  
    	  String sql = "SELECT userid, name FROM tuser WHERE userid=? AND pass=?";
    	  pstmt = conn.prepareStatement (sql);
    	  pstmt.setString (1, userid);
    	  pstmt.setString (2, password);
    	  
    	  // 3. 发送sql语句
    	     rs = pstmt.executeQuery();
    	  if (rs.next()) {
    	      flag = true;
    	  }
    } catch (Exception e) {
    } finally {
        // 4. 关闭连接
        try {
        	  rs.close();
	 pstmt.close();
        	 conn.close();
        } catch (Exception e) {}
    }