import java.sql.*; //這是連接中必須添加的
public class Data{
Connection conn;
Statement stat;
ResultSet rs;
Object body[][] = new Object[50][6];//根據自己創建的表來設定數組
public Data()
{
this.connection();
}
public void d1() {
try {
rs = stat.executeQuery("select * from student");//在oracle數據庫中創建了一個student表
for(int i=0;i<body.length;i++)
while (rs.next()) {
body[i][0] = rs.getString(1);
System.out.print(" "+body[i][0]);
body[i][1] = rs.getString(2);
System.out.print(" "+body[i][1]);
body[i][2] = rs.getInt(3);
System.out.print(" "+body[i][2]);
body[i][3] = rs.getString(4);
System.out.print(" "+body[i][3]);
body[i][4] = rs.getInt(5);
System.out.print(" "+body[i][4]);
body[i][5] = rs.getString(6);
System.out.print(" "+body[i][5]);
System.out.println();
}
} catch (SQLException ex) {
}
}
public void connection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); //加載驅動
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl"; //添加接口
conn = DriverManager.getConnection(url, "scott", "tiger");
//本人是在scott用戶下創建的表,所以要進入的是scott模式,以及它的登陸口令
if (conn == null)System.out.println("鏈接失敗");
else
System.out.println("鏈接成功");
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);//1、結果集的遊標可以上下移動,當數據庫變化時,當前數據不變
//不能用結果集更新數據庫中的表
} catch (Exception ex) {
}
}
public static void main(String[] args) {
Data data= new Data();
data.d1();
}
}