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:
- 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, user name, password
- String drivername="Oracle.jdbc.driver.OracleDriver";
- String url="Jdbc:oracle:thin: @localhost: 1521:bjpowernode"; //test is the database name, 1521 is the default port for connecting to the database
- String user="System"; //aa for 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 commands
- String sql = "SELECT * from t_user where user_id =?";
- //Create a 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");
- //Execute a query statement to save the data to the ResultSet object
- rs = Pstmt.executequery ();
- //Move the pointer to the next line to determine if there is data in the RS
- if (Rs.next ()) {
- //Output query Results
- SYSTEM.OUT.PRINTLN ("query to the name" "+ rs.getstring (" user_id ") + " ", The password is:" + rs.getstring ("password"));
- }else{
- //Output query Results
- System.out.println ("user name is not queried" "+ 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", 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