1. 程式人生 > >java oracle jdbc操作

java oracle jdbc操作

package com.test;

import java.sql.; import java.util.;

public class Test { public static final String DRIVER = “oracle.jdbc.driver.OracleDriver”;//oracle 驅動jar包在安裝目錄D:\ORACLE\product\11.2.0\dbhome_1\oui\jlib\classes12.jar public static final String URL = “jdbc:oracle:thin:@localhost:1521:orcl”; public static final String USERNAME = “wl”; public static final String PASSWORD = “123456”;

public static void main(String[] args) {
	Connection conn = null;
	try {

		Class.forName(DRIVER);// 找到oracle驅動器所在的類
		conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		StringBuffer sql = new StringBuffer();
		sql.append("select * from t_user");
		PreparedStatement ps = conn.prepareStatement(sql.toString());
		ResultSet rs = ps.executeQuery();

		while (rs.next()) {
			for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
				System.out.print(rs.getObject(i) + "\t");
			}
			System.out.println('\n');
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}