Experts for you to explain the basic Java database operations

Source: Internet
Author: User
Tags insert sql query rollback access database
Data | database | Detailed Java database BASIC operations
1. Java database Operation Basic process
2, a few commonly used important skills:
Scrollable, updated recordsets
Batch Update
Transaction processing

Java database Operation basic process: Get database connection-Execute SQL statement-Process Execution result-release database connection
1. Get the database connection
1) using DriverManager to Access database connection
Example
String classname,url,uid,pwd;
ClassName = "Oracle.jdbc.driver.OracleDriver";
url = "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORASVR;
UID = "System";
PWD = "Manager";
Class.forName (ClassName);
Connection cn = drivermanager.getconnection (URL,UID,PWD);
2 using Jndi (Java naming and directory services)
Example
String Jndi = "jdbc/db";
Context CTX = (context) new InitialContext (). Lookup ("java:comp/env");
DataSource ds = (DataSource) ctx.lookup (JNDI);
Connection cn = Ds.getconnection ();
More used in JSP

2. Execute SQL statement
1 using statement to execute SQL statements
String SQL;
Statement sm = cn.createstatement ();
Sm.executequery (SQL); Execute a Data query statement (SELECT)
Sm.executeupdate (SQL); Execute Data UPDATE statements (delete, update, insert, DROP, etc.) Statement.close ();
2 using PreparedStatement to execute SQL statements
String SQL;
sql = "INSERT into user (Id,name) VALUES (?,?)";
PreparedStatement PS = cn.preparestatement (SQL);
Ps.setint (1,XXX);
Ps.setstring (2,XXX);
...
ResultSet rs = Ps.executequery (); Inquire
int c = Ps.executeupdate (); Update

3, processing the results of implementation
Query statement that returns a recordset resultset
UPDATE statement, returning a number that represents the number of records affected by the update
The method of ResultSet
1, Next (), move the cursor back one row, or False if successful return true;
2, GETINT ("id") or getsting ("name"), returns the value of a field under the current cursor

4, Release the connection
Cn.close ();
Generally, close the resultset first, then close the statement (or PreparedStatement), and finally close the connection


Scrollable, updated recordset
 1, creating scrollable, updated Statement
  statement sm = cn.createstatement (resultset.type_ SCROLL_ENSITIVE,RESULTSET.CONCUR_READ_ONLY);
   The ResultSet that statement obtains is scrollable
 2, specifying parameters when creating PreparedStatement
   Preparedstatemet PS = cn.preparestatement (sql,resultset.type_scroll_insensitive,resultset.concur_read_only);

  resultset.absolute (9000);   
Batch update
 1, Statement
  statement sm = Cn.createstatement ();
  sm.addbatch (SQL1);
  sm.addbatch (SQL2);
  ..
  sm.executebatch ()
   A statement object that can execute multiple SQL statements after a batch update. The multiple statements can be delete, update, insert, or both
 2, PreparedStatement
  preparedstatement PS = Cn.preparedstatement (SQL);
  {
   ps.setxxx (1,xxx);
   
   ps.addbatch ();
  }
  ps.executebatch ();
   A PreparedStatement, you can put an SQL statement, transform the parameters to perform multiple times, one update.


Handling of transactions
1, turn off the automatic submission of connection
Cn.setautocommit (FALSE);
2. Execute a series of SQL statements
Important: Before executing each new SQL statement, the last statement (or Preparedstatemet) that executed the SQL statement must first close
Statement SM;
SM = cn.createstatement (insert into user ...);
Sm.executeupdate ();
Sm.close ();

SM = cn.createstatement ("INSERT INTO corp ...");
Sm.executeupdate ();
Sm.close ();

3. Submit
Cn.commit ();
4, if an exception occurs, then rollback
Cn.rollback ();



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.