[Java] 通过Java JDBC链接Oracle数据库 →→→→→进入此内容的聊天室

来自 , 2020-11-21, 写在 Java, 查看 153 次.
URL http://www.code666.cn/view/c44e5038
  1.  
  2. public class OracleJdbcTest  
  3. {  
  4. String driverClass =
  5.  
  6. "oracle.jdbc.driver.OracleDriver";  
  7.  
  8. Connection con;  
  9.  
  10. public void init
  11.  
  12.  
  13.  
  14.  
  15. {  
  16. Properties props = new Properties();  
  17.  
  18.  
  19. props.load(fs);  
  20.  
  21.  
  22. String url = props.getProperty
  23.  
  24. ("db.url");  
  25.  
  26. userName = props.getProperty("db.user");  
  27.  
  28.  
  29. String password = props.getProperty
  30.  
  31. ("db.password");  
  32. Class.forName(driverClass);  
  33.  
  34. con=DriverManager.getConnection(url,
  35.  
  36. userName, password);  
  37. }  
  38.  
  39. public void fetch() throws SQLException,
  40.  
  41. {  
  42.  
  43.  
  44.  
  45. con.prepareStatement("select SYSDATE from dual");  
  46.  
  47.  
  48. ResultSet rs = ps.executeQuery
  49.  
  50. ();  
  51.  
  52. while
  53.  
  54. (rs.next())  
  55. {  
  56.  
  57.  
  58. // do the
  59.  
  60. thing you do  
  61. }
  62.  
  63.    
  64. rs.close();  
  65.  
  66.  
  67. ps.close();  
  68.  
  69.  
  70. }  
  71.  
  72. public
  73.  
  74. static void main(String[] args)  
  75. {  
  76.  
  77.  
  78. OracleJdbcTest test = new
  79.  
  80. OracleJdbcTest();  
  81. test.init();  
  82. test.fetch
  83.  
  84. ();  
  85. }  
  86. }  
  87.  
  88.  
  89.  
  90. public class OracleJdbcTest
  91. {
  92. String driverClass =
  93.  
  94. "oracle.jdbc.driver.OracleDriver";
  95.  
  96.  
  97.  
  98.  
  99. public void init(FileInputStream fs) throws ClassNotFoundException,
  100.  
  101.  
  102. {
  103. Properties props = new Properties();
  104. props.load
  105.  
  106. (fs);
  107. String url = props.getProperty
  108.  
  109. ("db.url");
  110. String userName = props.getProperty
  111.  
  112. ("db.user");
  113. String password = props.getProperty
  114.  
  115. ("db.password");
  116. Class.forName(driverClass);
  117.  
  118.  
  119. con=DriverManager.getConnection(url, userName,
  120.  
  121. password);
  122. }
  123.  
  124.  
  125. public void fetch() throws SQLException, IOException
  126. {
  127. PreparedStatement ps = con.prepareStatement("select SYSDATE from
  128.  
  129.  
  130.  
  131. dual");
  132. ResultSet rs = ps.executeQuery();
  133.  
  134.  
  135. while (rs.next())
  136. {
  137. // do the
  138.  
  139. thing you do
  140. }
  141. rs.close();
  142. ps.close
  143.  
  144. ();
  145. }
  146.  
  147.  
  148. public static void main(String[] args)
  149. {
  150. OracleJdbcTest test = new OracleJdbcTest
  151.  
  152. ();
  153. test.init();
  154. test.fetch();
  155. }
  156. }

回复 "通过Java JDBC链接Oracle数据库"

这儿你可以回复上面这条便签

captcha