JDBC database API comparison instance learning

Source: Internet
Author: User


/* Function: batch process data in the database. 
For example, enter one thousand data records, it is obviously a waste of time to insert a record after each operation, 
such as creating a connection, and then disconnecting and then creating an insert, and. 
Of course, batch processing is not necessarily efficient, but it is a way to solve the problem. Time: 20131003 Author: 
*/public class PiChuLi {public static void main (String [] args) throws SQLException
 {// TODO Auto-generated method stubcreate ();} static void create () throws SQLException
 {Connection conn = null; PreparedStatement ps = null; ResultSet resultset = null;
 try {// 2. establish connection conn = JdbcUtils. getConnection (); 
// 3. create statement String SQL = "insert into user (name, birthday, money) values (?,?,?) "; 
Ps = conn. prepareStatement (SQL, Statement. RETURN_GENERATED_KEYS); 
for (int I = 0; I <1000; I ++) {ps. setString (1, "sdmf" + I);
 ps. setDate (2, new java. SQL. date (System. currentTimeMillis ();
ps. setFloat (3, 345? I =*ps.addbatch({**ps.exe cuteBatch ();}
 finally {JdbcUtils. free (resultset, ps, conn) ;}} 
seek/* function: Get the primary key of the newly inserted information. 
This is one of the APIs used to learn this method time: 20131003 Author: 
*/public class OtherApi {public static void main (String [] args) throws SQLException 
{int id = create (); System. out. println (id) ;}// the primary key static int create () throws SQLException {Connection conn = null;
 PreparedStatement ps = null; ResultSet resultset = null; try {// 2. establish connection conn = JdbcUtils. getConnection (); 
/ 3. create statement String SQL = "insert into user (name, birthday, money) values ('wy', '2017-09-23 ', '20170101')" cuteUpdate ();
 resultset = ps. getGeneratedKeys (); int id = 0; if (resultset. next () {id = resultset. getInt (1);} return id;} finally {JdbcUtils. free (resultset, ps, conn) ;}} 
seek/* function: Page query time of the result set instance that can be rolled: 20131003 Author: */public class ScrollAPIDemo {public static void main (String [] args) throws SQLException {scroll ();} // a scrollable result set instance 
// querying by PAGE static void scroll () throws SQLException {Connection conn = null; Statement st = null; ResultSet resultset = null; try {// 2. establish connection conn = JdbcUtils. getConnection (); // 3. create statement st = conn. createStatement (); 
// 4. execute the statement String SQL = "select id, name, birthday, money from user" using resultset1_st.exe cuteQuery (SQL); resultset. absolute (8); System. out. println (); if (resultset. previous () {System. out. println (resultset. getObject ("id") + "\ t" + resultset. getObject ("name") + "\ t" + resultset. getObject ("birthday") + "\ t" + resultset. getObject ("money") + "\ t ");} // One paging method, however, is less efficient. MySQL itself supports paging. // For MySQL, if paging is set, it is directly written as: 
// select id, name, birthday in the SQL statement, money from user limit 100th, 10 // that is, 10 data records are displayed in the resultset. absolute (100); 
int I = 0; while (resultset. next () & I <10) {I ++; System. out. println (resultset. getObject ("id") + "\ t" + resultset. getObject ("name") + "\ t" + resultset. getObject ("birthday") + "\ t" + resultset. getObject ("money") + "\ t") ;}} finally {JdbcUtils. free (resultset, st, conn) ;}} progress/* function: updatable result set time: 20131003 Author: */public class UpdateKeGengXin {/*** @ param args * @ throws SQLException */public static void main (String [] args) throws SQLException 
{// TODO Auto-generated method stubread ();} // updatable result set, which is updated after query.
 However, you can use static void read () throws SQLException {Connection conn = null; Statement st = null; ResultSet resultset = null;
 try {// 2. establish connection conn = JdbcUtils. getConnection (); 
// 3. create statement st = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE);
 // 4.execution sentence resultset1_st.exe cuteQuery ("select id, name, birthday, money from user where id <10");
 // 5. processing result while (resultset. next () {System. out. println (resultset. getObject ("id"); 
System. out. println (resultset. getObject ("name"); System. out. println (resultset. getObject ("birthday");
 System. out. println (resultset. getObject ("money"); String name = resultset. getString ("name"); if ("wangwu ". equals (name) 
{resultset. updateFloat ("money", 123); resultset. updateRow () ;}} finally {JdbcUtils. free (resultset, st, conn) ;}} 
metadata/* function: metadata information of the database time: 20131003 Author: 
*/public class DBMD {// original database information public static void main (String [] args)
 throws SQLException {Connection conn = JdbcUtils. getConnection (); 
DatabaseMetaData dbmd = conn. getMetaData (); System. out. println (dbmd. getDatabaseMajorVersion (); 
// database name System. out. println (dbmd. getDatabaseProductName ();
// database version number System. out. println (dbmd. getDatabaseProductVersion (); 
// whether transaction-type System is supported. out. println (dbmd. supportsTransactions ());}}


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.