Basic jdbc Learning

Source: Internet
Author: User
Package study; import java. SQL. *; import java. util. required; // public class StudyJdbc {// public static void main (String [] args) {// Connection conn = null; // Statement st = null; // ResultSet rs = null; // try {// Class. forName ("org. gjt. mm. mysql. driver "); // conn = DriverManager. getConnection (// "jdbc: mysql: // localhost: 3306/fly", "root", ""); // st = conn. createStatement (); // rs = st.exe cuteQuery ("select * from m Essage "); // while (rs. next () // System. out. println (rs. getInt ("id") + ":" // + rs. getString ("content"); //} catch (ClassNotFoundException e) {// e. printStackTrace (); // write it to the error log //} catch (SQLException e) {// e. printStackTrace (); //} finally {// try {// if (rs! = Null) // rs. close (); // if (st! = Null) // st. close (); // if (conn! = Null) // conn. close (); //} catch (SQLException e) {// e. printStackTrace (); /// // check the database content in eclipse // public class StudyJdbc {// PreparedStatment, filter some special characters to eliminate SQL injection and improve efficiency // public static void main (String [] args) {// Connection conn = null; // PreparedStatement st = null; // try {// Class. forName ("org. gjt. mm. mysql. driver "); // conn = DriverManager. getConnection (// "jdbc: mysql: // localhost: 3306/fly ", "Root", ""); // st = conn. prepareStatement ("insert into message values (null ,?, Current_timestamp )");//? Placeholder /// select * from message where id =? And content =? // St. setString (1, "hello"); // st.exe cuteUpdate (); //} catch (ClassNotFoundException e) {// e. printStackTrace (); // write it to the error log //} catch (SQLException e) {// e. printStackTrace (); //} finally {// try {// if (st! = Null) // st. close (); // if (conn! = Null) // conn. close (); //} catch (SQLException e) {// e. printStackTrace (); ///} // view the database content in eclipse // public class StudyJdbc {// batch processing // public static void main (String [] args) {// Connection conn = null; // PreparedStatement st = null; // try {// Class. forName ("org. gjt. mm. mysql. driver "); // conn = DriverManager. getConnection (// "jdbc: mysql: // localhost: 3306/fly", "root", ""); // st = conn. prepareStatement ("Insert into message values (null ,?, Current_timestamp )");//? Placeholder /// st. setString (1, "batch processing 1"); // st. addBatch (); // st. setString (1, "Batch Processing 2"); // st. addBatch (); // st. setString (1, "batch processing 3"); // st. addBatch (); // st.exe cuteBatch (); // when inserting three items together //} catch (ClassNotFoundException e) {/e. printStackTrace (); // write it to the error log //} catch (SQLException e) {// e. printStackTrace (); //} finally {// try {// if (st! = Null) // st. close (); // if (conn! = Null) // conn. close (); //} catch (SQLException e) {// e. printStackTrace (); ///} // view the database content in eclipse // public class StudyJdbc {// transaction processing // public static void main (String [] args) {// Connection conn = null; // PreparedStatement st = null; // try {// Class. forName ("org. gjt. mm. mysql. driver "); // conn = DriverManager. getConnection (// "jdbc: mysql: // localhost: 3306/fly", "root", ""); // conn. setAutoCommit (fa Lse); // automatically submit by default /// st = conn. prepareStatement ("insert into message values (null ,?, Current_timestamp )");//? Placeholder /// st. setString (1, "batch processing 1"); // st. addBatch (); // st. setString (1, "Batch Processing 2"); // st. addBatch (); // st. setString (1, "batch processing 3"); // st. addBatch (); // st.exe cuteBatch (); // three items are inserted together at the time of insertion. // conn. commit (); // confirm submission // conn. setAutoCommit (true); // restore automatic submission //} catch (ClassNotFoundException e) {// e. printStackTrace (); // write it to the error log //} catch (SQLException e) {// e. printStackTrace (); // try {// if (conn! = Null) // {// conn. rollback (); // roll back when an error occurs // conn. setAutoCommit (true); //} // catch (SQLException e1) // {// e1.printStackTrace (); ///} //} finally {// try {// if (st! = Null) // st. close (); // if (conn! = Null) // conn. close (); //} catch (SQLException e) {// e. printStackTrace (); ///} // view the database content in eclipse // public class StudyJdbc {// the handling process can be triggered. result set // public static void main (String [] args) {// Connection conn = null; // Statement st = null; // ResultSet rs = null; // try {// Class. forName ("org. gjt. mm. mysql. driver "); // conn = DriverManager. getConnection (// "jdbc: mysql: // localhost: 3306/fly", "root", ""); // required The number of lower keys supported by the development package and set/st = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY); // only available, cannot update // rs1_st.exe cuteQuery ("select * from message"); // rs. next (); // if the preceding two values are not specified, only rs can be used. next (); // System. out. println (rs. getInt ("id"); // rs. last (); // System. out. println (rs. getInt ("id"); // rs. previous (); // rs. absolute (8); //} catch (ClassNotFoundException e) {// e. printStackTrace (); // write it to the error log //} c Atch (SQLException e) {// e. printStackTrace (); //} finally {// try {// if (st! = Null) // st. close (); // if (conn! = Null) // conn. close (); //} catch (SQLException e) {// e. printStackTrace (); ///} // view the database content in eclipse // public class StudyJdbc {// The processing can be updated result set // public static void main (String [] args) {// Connection conn = null; // Statement st = null; // ResultSet rs = null; // try {// Class. forName ("org. gjt. mm. mysql. driver "); // conn = DriverManager. getConnection (// "jdbc: mysql: // localhost: 3306/fly", "root", ""); // required The number of lower keys supported by the development package and set/st = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_UPDATABLE); // only available, updatable // rs1_st.exe cuteQuery ("select * from message"); // rs. next (); // if the preceding two values are not specified, only rs can be used. next (); // update a row of Data // rs. updateString ("content", "updatable"); // updates data in the memory // rs. updateRow (); // update the database content ///// Insert a new row // rs. moveToInsertRow (); // rs. updateString ("content", "AAAA"); // rs. insertRow (); // move the cursor to the new row // rs. moveToInsertR Ow (); // Delete rows // rs. absolute (3); // rs. deleteRow (); //} catch (ClassNotFoundException e) {// e. printStackTrace (); // write it to the error log //} catch (SQLException e) {// e. printStackTrace (); //} finally {// try {// if (st! = Null) // st. close (); // if (conn! = Null) // conn. close (); //} catch (SQLException e) {// e. printStackTrace (); //} // view the database content in eclipse //}

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.