Simple jdbc additions and deletions to check the operation, with the source code

Source: Internet
Author: User

1  PackageCom.xyyz.jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 ImportJava.sql.ResultSet;7 8  Public classJdbcdemo {9 Ten      Public Static voidMain (string[] args)throwsException { One query (); A Insert (); - query (); - update (); the query (); - Delete (); - query (); -     } +  -     /** + * Query Code A      *  at      * @throwsException -      */ -      Public Static voidQuery ()throwsException { -         //Registration Driver -Class.forName ("Com.mysql.jdbc.Driver"); -         //parameters required for connecting data inString url = "Jdbc:mysql:///jdbctestdata"; -String user = "root"; toString password = "root"; +         //Get database connection -Connection Connection =drivermanager.getconnection (URL, user, password); theString sql = "SELECT * FROM Jdbctestdata"; *         //Get Precompiled Objects $PreparedStatement preparestatement =connection.preparestatement (SQL);Panax Notoginseng         //Execute SQL statement -ResultSet ResultSet =preparestatement.executequery (); the         //traversing print SQL statements +          while(Resultset.next ()) { ASystem.out.println (Resultset.getint (1) + "--" + resultset.getstring (2)); the         } +         //turn off freeing resources - resultset.close (); $ preparestatement.close (); $ connection.close (); -     } -  the     /** - * Insert DataWuyi      */ the      Public Static voidInsert ()throwsException { -         //Registration Driver WuClass.forName ("Com.mysql.jdbc.Driver"); -         //parameters required for connecting data AboutString url = "Jdbc:mysql:///jdbctestdata"; $String user = "root"; -String password = "root"; -         //Get database connection -Connection Connection =drivermanager.getconnection (URL, user, password); AString sql = "INSERT into Jdbctestdata value (?,?,?)"; +         //Get Precompiled Objects thePreparedStatement Prepare =connection.preparestatement (SQL); -Prepare.setstring (1, "6"); $Prepare.setstring (2, "Little white"); thePrepare.setstring (3, "30"); the         inti =prepare.executeupdate (); theSystem.out.println ("i=" +i); the         //Execute SQL statement - prepare.close (); in connection.close (); the     } the  About     /** the * Update Data the      */ the      Public Static voidUpdate ()throwsException { +         //Registration Driver -Class.forName ("Com.mysql.jdbc.Driver"); the         //parameters required for connecting dataBayiString url = "Jdbc:mysql:///jdbctestdata"; theString user = "root"; theString password = "root"; -         //Get database connection -Connection Connection =drivermanager.getconnection (URL, user, password); theString sql = "Update jdbctestdata set name=?", age=? where id=? "; the         //Get Precompiled Objects thePreparedStatement Prepare =connection.preparestatement (SQL); thePrepare.setstring (1, "Little Red"); -Prepare.setstring (2, "20"); thePrepare.setstring (3, "6"); the         inti =prepare.executeupdate (); theSystem.out.println ("i=" +i);94         //Execute SQL statement the prepare.close (); the connection.close (); the     }98  About     /** - * Delete Data101      */102      Public Static voidDelete ()throwsException {103         //Registration Driver104Class.forName ("Com.mysql.jdbc.Driver"); the         //parameters required for connecting data106String url = "Jdbc:mysql:///jdbctestdata";107String user = "root";108String password = "root";109         //Get database connection theConnection Connection =drivermanager.getconnection (URL, user, password);111String sql = "Delete from jdbctestdata where id =?"; the         //Get Precompiled Objects113PreparedStatement Prepare =connection.preparestatement (SQL); thePrepare.setstring (1, "6"); the         inti =prepare.executeupdate (); theSystem.out.println ("i=" +i);117         //Execute SQL statement118 prepare.close ();119 connection.close (); -     }121 122}

http://download.csdn.net/detail/u010798073/9888901

http://download.csdn.net/detail/u010798073/9888913

Simple jdbc additions and deletions to check the operation, with the source code

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.