Connect Java to a simple Oracle database instance

Source: Internet
Author: User

Database operations are an essential part of system development at present, especially in the big data era. But do you really know how Java connects to databases?


Let's give you a simple example of 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 the 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 indicates the Database Name and 1521 indicates the connection. The default database port string user = "system"; // AA is the username string Password = "bjpowernode"; // 123 is the password preparedstatement pstmt = NULL; resultset rs = NULL; // database connection object connection conn = NULL; try {// reflection class of the Oracle database driver. forname (drivername); // obtain the database connection conn = drivermanager. getconnection (URL, user, password); // The Output Database connects to the system. out. println (conn); // custom SQL command string SQL = "select * From t_user where user_id =? "; // Create the preparedstatement object pstmt = Conn. preparestatement (SQL); // pass the first parameter value root instead of the first question mark pstmt. setstring (1, "root"); // execute the query statement and save the data to the resultset object rs = pstmt.exe cutequery (); // move the pointer to the next row, determine whether the RS contains data if (RS. next () {// output the query result system. out. println ("the query name is [" + Rs. getstring ("user_id") + ", whose password is:" + Rs. getstring ("password");} else {// output the query result system. out. println ("the User Name Not found is [" + Rs. getstring ("user_id") + "] information") ;}} catch (Cl Assnotfoundexception 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 and select "Run as" => "Java application" to run the program segment. The running result is displayed on the console, if a string similar to "oracle. JDBC. driver. t4cconnection @ 7c242f04 "indicates that the connection is successful. Running result



Next, let's take a simple anatomy of this procedure.

This program is an example of connecting Java to the Oracle database and uses JDBC to connect to the database. Therefore, you need to introduce ojdbc14.jar. Before performing this operation, you must first get the database Driver Class Object and get the database connection object through the driver object. Among them, class. forname (drivername) is the application class reflection mechanism that loads the driver. Drivermanager
Class is the management layer of JDBC, acting between the user and the driver. It tracks available drivers and establishes connections between the database and the corresponding drivers. Generally, you only need to directly use the drivermanager. getconnection method in the class to establish a connection with the database.

The preparedstatement interface inherits statement and is a class used to perform database operations. Preparedstatement is much more efficient than statement in multiple calls, so many people advocate replacing statement with preparedstatement. In the next blog, we will introduce this article in detail.
Understand statement and preparedstatement. Preparedstatement can be viewed as the command class in. net.

The resultset interface is used in many languages to store the queried data. After data is queried, the next () method is usually used in Java to read data.





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.