导航

Java代码片段——使用JDBC链接Oracle

Posted on 2017-07-02 16:11  天一涯  阅读(180)  评论(0编辑  收藏  举报
 1 public class OracleJdbcTest{
 2     String driverClass = ”oracle.jdbc.driver.OracleDriver”;
 3     Connection con;
 4     public void init(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException
 5     {
 6         Properties props = new Properties();
 7         props.load(fs);
 8         String url = props.getProperty(“db.url”);
 9         String userName = props.getProperty(“db.user”);
10         String password = props.getProperty(“db.password”);
11         Class.forName(driverClass);
12         con=DriverManager.getConnection(url, userName, password);
13     }
14     public void fetch() throws SQLException, IOException
15     {
16         PreparedStatement ps = con.prepareStatement(“select SYSDATE from dual”);
17         ResultSet rs = ps.executeQuery();
18         while (rs.next())
19         {
20             // do the thing you do
21         }
22         rs.close();
23         ps.close();
24     }
25     public static void main(String[] args)
26     {
27         OracleJdbcTest test = new OracleJdbcTest();
28         test.init();
29         test.fetch();
30     }
31 }