日期:2014-05-16 浏览次数:20381 次
package jdbc.test; import java.sql.*; public class PreparedStatement { public static void main(String[] args){ String dbDriver = "oracle.jdbc.driver.OracleDriver"; String dbUrl = "jdbc:oracle:thin:@localhost:1521:orcl"; String dbUser = "scott"; String dbPwd = "tiger"; try{ String sql = "select * from emp"; //explicitly load the driver before get a DB connection Class.forName(dbDriver).newInstance(); //connection to db Connection conn = DriverManager.getConnection(dbUrl,dbUser,dbPwd); //create a statment object Statement stmt = conn.createStatement(); //get a ResultSet object ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString(4) + "\t" + rs.getString(5) + "\t" + rs.getString(6) + "\t" + rs.getString(7) + "\t" + rs.getString(8) ); } }catch(Exception e){ e.printStackTrace(); } } }