The operation of the database is one of the necessary development parts of the current system development, especially in the large data age, the database is especially important. But do you really understand how Java and the database are connected?
Let's give you a simple example of a database connection:
Package com.java.dbtest;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException; public class Testconnection implements dbtest{public void Selectuser () {//Set database driver, database connection address, port, name, username, password String driverna
Me= "Oracle.jdbc.driver.OracleDriver"; String url= "Jdbc:oracle:thin: @localhost: 1521:bjpowernode"; Test is the database name, 1521 is the default port String user= "system" for connecting to the database; AA is the user name String password= "Bjpowernode";
123 for password PreparedStatement pstmt = null;
ResultSet rs = null;
Database Connection object Connection conn = null;
try {//Reflection Oracle Database Driver Class Class.forName (drivername);
Get database Connection conn = drivermanager.getconnection (URL, user, password);
Output database connection SYSTEM.OUT.PRINTLN (conn);
Custom SQL command String sql = "SELECT * from t_user where user_id =?";
Create the PreparedStatement object under this connection pstmt = conn.preparestatement (sql);
Pass the first parameter value root, instead of the first question mark Pstmt.setstring (1, "root"); Executes the query statement and saves the data to the ResultSet object rs = Pstmt.executequery (); Move the pointer to the next line to determine if there is data in RS (Rs.next ()) {//Output query Results System.out.println ("Query to name" + rs.getstring ("user_id") + "", The password is: "+ RS
. getString ("password"));
}else{//Output query Results System.out.println ("not queried to User name" "+ rs.getstring (" user_id ") +" "information);
} catch (ClassNotFoundException e) {e.printstacktrace ();
catch (SQLException e) {e.printstacktrace ();
}finally{try{if (rs!= null) {rs.close ();
} if (pstmt!= null) {pstmt.close ();
} if (conn!= null) {conn.close ();
} catch (SQLException e) {e.printstacktrace (); }} public static void Main (string[] args) {new testconnection ().
Selectuser (); }
}
In the main function, right-click, select "Run as" => "Java Application", run the program segment, in the console, you can see the results of the run, if given a string similar to " Oracle.jdbc.driver.t4cconnection@7c242f04 "Such a string, it means that you connect successfully. Run the results as shown in figure:
Now let's dissect the procedure briefly.
This program is the Java connection Oracle database instance, uses the JDBC to complete the connection database operation, therefore needs to introduce the Ojdbc14.jar. Before the operation, we must first get the object of the database-driven class, and get the database connection object through the driver object.
where Class.forName (drivername) is the application of class reflection mechanism to load drivers. The DriverManager class is a JDBC management layer that acts between users and drivers. It tracks the available drivers and establishes a connection between the database and the corresponding drivers.
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 more efficient than statement in multiple calls, so many people advocate preparedstatement instead of statement.
In the next blog post, we will give you a detailed description of this in-depth understanding of Statement and PreparedStatement. PreparedStatement can be viewed as a class of command in. Net.
The ResultSet interface appears in many languages, and it mainly stores the data that is queried. Each time the data is queried, the next () method is usually used in the Java language to read the data.