JDBC Connect Oracle

Source: Internet
Author: User

Database operation is one of the necessary development parts of current system development, especially in today's big Data era, database is especially important. But do you really know how Java is connected to the database?

Let's give you a simple example of a database connection:

  1. Package com.java.dbtest;
  2. Import java.sql.Connection;
  3. Import Java.sql.DriverManager;
  4. Import java.sql.PreparedStatement;
  5. Import Java.sql.ResultSet;
  6. Import java.sql.SQLException;
  7. Public class Testconnection implements dbtest{
  8. public void Selectuser () {
  9. //Set database driver, database connection address, port, name, user name, password
  10. String drivername="Oracle.jdbc.driver.OracleDriver";
  11. String url="Jdbc:oracle:thin: @localhost: 1521:bjpowernode"; //test is the database name, 1521 is the default port for connecting to the database
  12. String user="System"; //aa for user name
  13. String password="Bjpowernode"; //123 for password
  14. PreparedStatement pstmt = null;
  15. ResultSet rs = null;
  16. //Database Connection object
  17. Connection conn = null;
  18. try {
  19. //Reflection Oracle Database driver class
  20. Class.forName (drivername);
  21. //Get database connection
  22. conn = drivermanager.getconnection (URL, user, password);
  23. //Output database connection
  24. SYSTEM.OUT.PRINTLN (conn);
  25. //Custom SQL commands
  26. String sql = "SELECT * from t_user where user_id =?";
  27. //Create a PreparedStatement object under this connection
  28. pstmt = conn.preparestatement (sql);
  29. //Pass the first parameter value root, instead of the first question mark
  30. Pstmt.setstring (1, "root");
  31. //Execute a query statement to save the data to the ResultSet object
  32. rs = Pstmt.executequery ();
  33. //Move the pointer to the next line to determine if there is data in the RS
  34. if (Rs.next ()) {
  35. //Output query Results
  36. SYSTEM.OUT.PRINTLN ("query to the name" "+ rs.getstring (" user_id ") + " ", The password is:" + rs.getstring ("password"));
  37. }else{
  38. //Output query Results
  39. System.out.println ("user name is not queried" "+ rs.getstring (" user_id ") + " "Information");
  40. }
  41. } catch (ClassNotFoundException e) {
  42. E.printstacktrace ();
  43. } catch (SQLException e) {
  44. E.printstacktrace ();
  45. }finally{
  46. try{
  47. if (rs! = null) {
  48. Rs.close ();
  49. }
  50. if (pstmt! = null) {
  51. Pstmt.close ();
  52. }
  53. if (conn! = null) {
  54. Conn.close ();
  55. }
  56. } catch (SQLException e) {
  57. E.printstacktrace ();
  58. }
  59. }
  60. }
  61. public static void Main (string[] args) {
  62. new Testconnection ().  Selectuser ();
  63. }
  64. }



In the main function, right-click, select "Run as" + "Java Application", will run the program segment, in the console, you can see the results of the run, if you give a string similar to "[E-mail protected]", It means that you are connected successfully. Run results

Let's simply dissect this procedure.

This program is a Java connection to the Oracle database instance, the use of JDBC to complete the operation to connect the database, so need to introduce Ojdbc14.jar. Before the operation, we first have to get the object of the database driver class and get the database connection object by driving the object. where Class.forName (drivername) is the application of the class reflection mechanism to load the driver. The DriverManager class is the management layer of JDBC, which acts between the user and the driver. It tracks the available drivers and establishes a connection between the database and the appropriate driver. You can establish a connection to a database simply by using the method Drivermanager.getconnection directly in the class

The PreparedStatement interface inherits statement, which is the class used to perform database operations. PreparedStatement is much more efficient than statement in multiple calls, so many people are advocating preparedstatement instead of statement. In the next blog post, we'll cover this in more detail, "in-depth understanding of Statement and PreparedStatement." PreparedStatement can be seen as a class of command in. Net.

The ResultSet interface has been present in many languages, primarily to store the data being queried. After each query to the data, the Java language typically uses the next () method to read the data.

JDBC Connect Oracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.