JDBC Learning Notes

Source: Internet
Author: User
Tags bulk insert

Database Data Update Operation statement interface operation: "Code"

Import Java.sql.DriverManager;
Import java.sql.SQLException;

public class Linkupdate {
Private String driver= "Oracle.jdbc.OracleDriver";
Private String url= "Jdbc:oracle:thin: @localhost: 1521:xe";
Private String user= "Victor";
Private String password= "Victor";
public void Link (String name) {
Java.sql.Connection Con=null;
Java.sql.Statement Sta=null;
Java.sql.ResultSet Res=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
Sta=con.createstatement ();
String sql= "Update info set name= '" +name+ "' WHERE name= ' * * * ';
Res=sta.executequery (SQL);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (res!=null)
Res.close ();
if (sta!=null)
Sta.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
public static void Main (string[] args) {
Linkupdate l= new Linkupdate ();
L.link ("* * *");
}
}

Database data Operation Preparestatement Interface: "Code"

Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import Java.util.Date;
public class Dislink {
Private String driver= "Oracle.jdbc.OracleDriver";
Private String url= "Jdbc:oracle:thin: @localhost: 1521:xe";
Private String user= "Victor";
Private String password= "Victor";
Pre-compiled Preparestatement interface INSERT statement "with parameters"
/*public void link (int id,string name,int age,string phone,string gender) {
Java.sql.Connection Con=null;
Java.sql.PreparedStatement Pre=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
System.out.println (con);
To create a pre-compiled preparestatement
String sql= "INSERT into info values (" +id+ ", '" +name+ "', '" +age+ "', '" +phone+ "', '" +gender+ "')";
Pre=con.preparestatement (SQL);
Pre.execute ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (pre!=null)
Pre.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}*/
Pre-compiled Preparestatement interface INSERT statement, insert single object data "heterogeneous" "using placeholders (without parameters)"
/*public void Link1 () {
Java.sql.Connection Con=null;
Java.sql.PreparedStatement Pre=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
String sql= "INSERT into info values (?,?,?,?,?)";
Pre=con.preparestatement (SQL);
Pre.setint (1, 4);
Pre.setstring (2, "BABC4");
Pre.setint (3, 18);
Pre.setstring (4, "1563");
Pre.setstring (5, "male");
Pre.execute ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (pre!=null)
Pre.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}*/
Pre-compiled Preparestatement interface INSERT statement, insert Bulk object data "isomorphic" "using placeholders (without parameters)"
/*public void Link2 () {
Java.sql.Connection Con=null;
Java.sql.PreparedStatement Pre=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
String sql= "INSERT into info values (?,?,?,?,?)";
Pre=con.preparestatement (SQL);
for (int i=1;i<=20;i++) {
Pre.setint (1, i);
Pre.setstring (2, "abc" +i);
Pre.setint (3, 9+i);
Pre.setstring (4, "153");
Pre.setstring (5, "male");
Pre.execute ();
}
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (pre!=null)
Pre.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}*/
Pre-compiled Preparestatement interface Bulk INSERT data, manually commit the transaction "* * * * * * * * * *", the output program run time, the SQL statement to insert the bulk data objects are saved to the cache for conditional judgment operation
/*public void Link3 () {
Java.sql.Connection Con=null;
Java.sql.PreparedStatement Pre=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
Con.setautocommit (FALSE);
String sql= "INSERT into info values (?,?,?,?,?)";
Pre=con.preparestatement (SQL);
Long Start_date=system.currenttimemillis ();
for (int i=51;i<=100;i++) {
Pre.setint (1, i);
Pre.setstring (2, "abc" +i);
Pre.setint (3, 30+i);
Pre.setstring (4, "15163");
Pre.setstring (5, "male");
Save all the preceding SQL statements in the cache
Pre.addbatch ();
if (i%10==0) {
Pre.executebatch ();
Con.commit ();
}
}
Pre.executebatch ();
Con.commit ();
Long Time_end=system.currenttimemillis ();
System.out.println (time_end-start_date);
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (pre!=null)
Pre.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}*/
Pre-compiled Preparestatement interface for querying database data "without parameters"
/*public void Link4 () {
Java.sql.Connection Con=null;
Java.sql.PreparedStatement Pre=null;
Java.sql.ResultSet Res=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
String sql= "SELECT * from info where id=1";
Pre=con.preparestatement (SQL);
Res=pre.executequery ();
while (Res.next ()) {
System.out.println ("id=" +res.getint (1) + "; "+
"Name=" +res.getstring (2) + "; "+
"Age" +res.getint (3) + "; "+
"Phone" +res.getstring (4) + "; "+
"Gender=" +res.getstring (5));
}
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (res!=null)
Res.close ();
if (pre!=null)
Pre.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}*/
Pre-compiled Preparestatement interface for querying database data "without parameters"
/*public void link5 (int id) {
Java.sql.Connection Con=null;
Java.sql.PreparedStatement Pre=null;
Java.sql.ResultSet Res=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
String sql= "SELECT * from info where id=?";
Pre=con.preparestatement (SQL);
Pre.setint (1, id);
Res=pre.executequery ();
while (Res.next ()) {
System.out.println ("id=" +res.getint (1) + "; "+
"Name=" +res.getstring (2) + "; "+
"Age" +res.getint (3) + "; "+
"Phone" +res.getstring (4) + "; "+
"Gender=" +res.getstring (5));
}
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (pre!=null)
Pre.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}*/
Pre-compiled Preparestatement interface INSERT statement "with parameters", "with placeholders"
/*public void link6 (int id,string name,int age,string phone,string gender) {
Java.sql.Connection Con=null;
Java.sql.PreparedStatement Pre=null;
Java.sql.ResultSet Res=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
String sql= "INSERT into info values (?,?,?,?,?)";
Pre=con.preparestatement (SQL);
Pre.setint (1,id);
Pre.setstring (2,name);
Pre.setint (3,age);
Pre.setstring (4,phone);
Pre.setstring (5,gender);
Pre.executequery ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (res!=null)
Res.close ();
if (pre!=null)
Pre.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}*/
Pre-compiled Preparestatement interface INSERT statement [how time is handled] (no parameters passed in)
public void Link7 () {
Java.sql.Connection Con=null;
Java.sql.PreparedStatement Pre=null;
Java.sql.ResultSet Res=null;
try {
Class.forName (driver);
Con=drivermanager.getconnection (URL, user, password);
String sql= "INSERT into data values (?,?,?)";
Pre=con.preparestatement (SQL);
Pre.setint (1, 1);
Pre.setstring (2, "* * *");
Date Date=new date ();
Pre.setdate (3, New Java.sql.Date (Date.gettime ()));
Pre.setdate (3,new java.sql.Date (Date.gettime ()));
Res=pre.executequery ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (res!=null)
Res.close ();
if (pre!=null)
Pre.close ();
if (con!=null)
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
public static void Main (string[] args) {
Dislink dis=new Dislink ();
Dis.link (3, "***3", +, "163", "male");
Dis.link1 ();
Dis.link2 ();
Dis.link3 ();
Dis.link4 ();
Dis.link6 (103, ***103 ",", "1563", "male");
Dis.link7 ();
}
}

JDBC Learning Notes

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.