How to use JDBC to connect and manipulate Oracle databases

Source: Internet
Author: User
Tags odbc stmt

Before learning. NET. Previously connected to the database using ODBC, and in Java is usually used in the JDBC Connection database, here is an example of Oracle database for a simple summary of how to use JDBC to connect and manipulate the database.

1. Connection

public class Dbutil {public static Connection getconnection () {Connection conn=null;try {class.forname (" Oracle.jdbc.driver.OracleDriver ");//Find the class where the Oracle drive is located string url=" Jdbc:oracle:thin: @localhost: 1521:bjpowernode "; URL address string username= "DRP"; String password= "DRP"; conn=drivermanager.getconnection (URL, username, password);} catch (ClassNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (SQLException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} Return conn;} <pre name= "code" class= "java" >public static void Close (PreparedStatement pstmt) {if (pstmt!=null) {try { Pstmt.close ();} catch (SQLException e) {e.printstacktrace ();}}} public static void Close (ResultSet rs) {if (Rs!=null) {try {rs.close ()} catch (SQLException e) {//TODO auto-generated CATC H Blocke.printstacktrace ();}}}
}


When running class.forname, you need to find the address where the Oracledriver is located, with the path:

1, first find

D:\oracle\product\10.2.0\db_1\jdbc\lib found Ojdbc14.jar .

2. Second, find the ojdbc14.jar\oracle\jdbc\driver below oraceldriver so that you find the driver file to use


2. Operation Database--Join

public void AddUser (user user) {String sql= ' insert into T_user (user_id,user_name,password,contact_tel,email,create_ DATE) VALUES (?

,?

,?,?,?

,?)"; Connection conn=null for the participant placeholder; PreparedStatement Pstmt=null; Typically, PreparedStatement is used to operate and performance is optimized try{conn=dbutil.getconnection ();p stmt=conn.preparestatement (SQL); Pstmt.setstring (1, User.getuserid ());p stmt.setstring (2,user.getusername ());p stmt.setstring (3, User.getPassword () );p stmt.setstring (4, User.getcontacttel ());p stmt.setstring (5,user.getemail ());//pstmt.settimestamp (6,new Timestamp (System.currenttimemillis ()));p Stmt.settimestamp (6, New Timestamp (New Date (). GetTime ());// Get current system time pstmt.executeupdate ();//Run additions and deletions}catch (SQLException e) {e.printstacktrace ();} Finally{dbutil.close (conn);D Butil.close (pstmt);}}


3. Operation Database--Query

Public User Finduserbyid (string userId) {String sql = "Select user_id, user_name, password, contact_tel, email, create_date From T_user where user_id=? "; Connection Conn=null; PreparedStatement Pstmt=null; ResultSet rs=null;//Defines the result set that holds the result of the query user user=null;try{conn=dbutil.getconnection ();p stmt=conn.preparestatement (SQL); Pstmt.setstring (1,userid); Rs=pstmt.executequery ();//Run Query operation if (Rs.next ()) {user=new user (); User.setuserid ( Rs.getstring ("user_id")), User.setusername (rs.getstring ("user_name")), User.setpassword (rs.getstring ("password") ); User.setcontacttel (rs.getstring ("Contact_tel")); User.setemail (rs.getstring ("email")); User.setcreatedate ( Rs.gettimestamp ("Create_date"));}} catch (SQLException e) {e.printstacktrace ();} finally{//is closed in order Dbutil.close (RS);D butil.close (pstmt);D butil.close (conn);} return user;}

four . Summary1. Simple differences between PreparedStatement and statement statement generates a run plan for an SQL statement, assuming the values are different. A different SQL statement is generated, and the number of times the corresponding number of parameters is run. Assuming that there is only one SQL statement running, it is best to use statement. PreparedStatement uses binding variables to reuse the run plan. The corresponding query statements for the different parameters. Only one SQL statement is generated. Greatly improve the efficiency of the operation. is pre-compiled. Increased efficiency in high-volume statement operations, at the same time using '? ' To represent the number of parameters that can prevent SQL injection. Higher security.

2, with the previous knowledge to contact The connection objects in the currently exposed JDBC are connection this is the same as the connection of the previous ODBC, which is the database connection object. Praparedstatement and statement are similar to command objects in ODBC. are used to run SQL statements.

The resultset used in the query method is similar to the previously used dataset or DataTable function, and this connection seems to be a lot easier to connect and use.



How to use JDBC to connect and manipulate Oracle databases

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.