Jdbc:mysql and Oracle Insert a piece of data back to the primary key

Source: Internet
Author: User

Package org.sin.common.dao;import java.sql.callablestatement;import java.sql.connection;import  java.sql.drivermanager;import java.sql.preparedstatement;import java.sql.resultset;import  java.sql.SQLException;import java.sql.Statement;import oracle.jdbc.OracleTypes;import  org.sin.domain.user;public class jdbcdao {private static boolean mysql =  false;private Connection conn;static {try {if  (MySQL)  {class.forname (" Com.mysql.jdbc.Driver ");}  else {class.forname ("Oracle.jdbc.driver.OracleDriver");}}  catch  (classnotfoundexception e)  {e.printstacktrace ();}} Private jdbcdao ()  {String url, user, password;if  (MySQL)  {url =  " Jdbc:mysql://127.0.0.1:3306/cl ";user = " root ";p assword = ";}  else {url =  "Jdbc:oracle:thin: @localhost: 1521:orcl";user =  "Andy";p Assword =  "root";} Try {conn = drivermanager.getconnection (Url, user, password); System.out.println (Conn.getautocommit ());}  catch  (sqlexception e)  {e.printstacktrace ();}} Private user saveuser (User user)  {String sql =  "Insert into t_ User (Id,username,password)  values (User_id.nextval,?,?) "; Long id = -1l;try {conn.setautocommit (FALSE); Preparedstatement ps = conn.preparestatement (sql, statement.return_generated_keys); int  i = 0;for  (i = 0; i < 10000; i++)  {ps.setstring (1,  user.getusername ()  +  "-"  + i);p s.setstring (2, user.getpassword ()  +  "- " + i");p s.executeupdate (); Resultset rs = ps.getgeneratedkeys ();while  (Rs.next ())  {id = rs.getlong (1); System.out.println (">>>>>>>"  + id);}} Conn.commit ();}  catch  (sqlexception e)  {e.printstacktrace ();} User.setid (ID); return user;} Private user saveuser2 (User user)  {String sql =  "begin insert  Into t_user (Id,username,password)  values (User_id.nextval,?,?)  returning id into ?;  END; "; Long id = -1l;try {conn.setautocommit (FALSE); Callablestatement cs = conn.preparecall (SQL); for (int i=0;i<10000;i++) {cs.setString (1,  user.getusername ()  +  "-"  + i) cs.setstring (2, user.getpassword ()  +  "- " + i"); Cs.registeroutparameter (3, oracletypes.number); Cs.execute (); Id = cs.getlong (3) ;} Conn.commit ();}  catch  (sqlexception e)  {e.printstacktrace ();} User.setid (ID); return user;} Public static void main (String[] args)  {jdbcdao dao = new jdbcdao (); Long start = system.currenttimEmillis (); User u = new user ("Andy",  "Andypwd");d AO. SaveUser2 (U); Long end = system.currenttimemillis (); System.out.println (U.getid ()  +  "--"  + u.getusername ()); long k = end - start; System.out.println ("Consumption:"  + k);}}


This article is from the "Sky Sea Wide" blog, please be sure to keep this source http://ether007.blog.51cto.com/8912105/1536121

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.