package oracleConn; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Execute { public static final String DBDRIVER="oracle.jdbc.driver.OracleDriver"; public static final String DBURL="jdbc:oracle:thin:@localhost:1521:orcl";//数据库连接地址,orcl为全局数据库名 public static final String DBUSER="system";//用户名 public static final String DBPASSWORD="admin";//密码 public static void main(String[] args) throws Exception{ Connection conn=null; ResultSet rs=null; PreparedStatement pstmt=null; Class.forName(DBDRIVER); // Oracle驱动加载 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//取得数据库连接 /* *以下部分作为数据库连接测试 *在orcl中建立了一个名为mysql的数据表 *内容仅有id和name */ String sql="SELECT * FROM myoracle"; //数据库查询 pstmt=conn.prepareStatement(sql); rs=pstmt.executeQuery(); while(rs.next()){ String id=rs.getString("id"); String name=rs.getString("name"); System.out.print("编号:"+id+"; "); System.out.print("姓名:"+name+"; "); } rs.close(); pstmt.close(); conn.close(); } }