java连接oracle

来源:互联网 发布:部落冲突骷髅法术数据 编辑:程序博客网 时间:2024/06/05 08:27
import java.sql.*; public class JDBCExample { public static void main(String[] args) {  try {   Class.forName("oracle.jdbc.driver.OracleDriver");   String url = "jdbc:oracle:thin:@localhost:1522:lwb";   Connection conn = DriverManager     .getConnection(url, "scott", "tiger");   Statement stmt = conn.createStatement();   ResultSet rs = stmt.executeQuery("select * from dept");   while (rs.next()) {    System.out.print("DeptNo:" + rs.getInt(1));    System.out.print("/tDeptName:" + rs.getString(2));    System.out.println("/tLOC:" + rs.getString(3));   }   rs.close();   stmt.close();   conn.close();  } catch (ClassNotFoundException e) {   System.out.println("找不到指定的驱动程序类");  } catch (SQLException e) {   e.printStackTrace();  } catch (Exception ex) {   ex.printStackTrace();  } } }


 

注意:

  运行前先要查看oracle的端口,用户名,密码是否正确