1.数据源连接,要新建数据源TestDataBase package com.shenjun; import java.sql.*; public class conn { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection ct = DriverManager.getConnection("jdbc:odbc:TestDataBase","scott","tiger"); Statement sm = ct.createStatement(); ResultSet rs = sm.executeQuery("select * from student"); while(rs.next()){ System.out.println("用户名:"+rs.getString(2)); } }catch(Exception e){ e.printStackTrace(); } } } 2.jdbc连接,引jar包 package com.shenjun; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class conn2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection ct = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott","tiger"); Statement sm = ct.createStatement(); ResultSet rs = sm.executeQuery("select * from student"); while(rs.next()){ System.out.println("用户名:"+rs.getString(2)); } }catch(Exception e){ e.printStackTrace(); } } }
oracle与java的连接2
最新推荐文章于 2023-11-17 13:29:40 发布