Java 连接Oracle

清疚 2022-08-06 14:07 182阅读 0赞

需要把oracle/common/lib/classes12.zip 复制到工程的lib中。

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class test {

  1. /\*\*
  2. \* @param args
  3. \*/
  4. public static void main(String\[\] args) \{
  5. // TODO Auto-generated method stub

// System.out.print(“hello”);
// InputStream a = (test.class).getResourceAsStream(“messages_ja.properties”);
Connection conn= getConnection();
try {
Statement stmt = conn.createStatement();

  1. ResultSet rs = stmt.executeQuery("select \* from et\_boshyoujiinfo");
  2. \} catch (SQLException e) \{
  3. // TODO Auto-generated catch block
  4. e.printStackTrace();
  5. \}
  6. \}
  7. public static Connection getConnection()
  8. \{
  9. Connection con = null;
  10. String url = "jdbc:oracle:thin:@192.168.1.2:1521:ORCL";
  11. String userName ="scott";
  12. String password ="tiger";
  13. String driver="oracle.jdbc.driver.OracleDriver";
  14. try
  15. \{
  16. Class.forName(driver);
  17. con = DriverManager.getConnection(url, userName, password);
  18. \}
  19. catch(SQLException e)
  20. \{
  21. e.printStackTrace();
  22. \}
  23. catch(ClassNotFoundException ex)
  24. \{
  25. ex.printStackTrace();
  26. \}
  27. return con;
  28. \}

}

发表评论

表情:
评论列表 (有 0 条评论,182人围观)

还没有评论,来说两句吧...

相关阅读