JDBC Advanced PreparedStatement Execute SQL statement (MySQL)

Source: Internet
Author: User

first, what is PreparedStatementIn the Java API documentation, we can see that PreparedStatement is the statement sub-interface () object that represents the precompiled SQL statement, and the SQL statement is precompiled and stored in the PreparedStatementObject. You can then use this object to execute the statement efficiently several times. second, by PreparedStatement get parameters executed on the Run command line, insert parameters into a datasheetThe relevant experimental process, including the pre-creation of the database required by the program, the creation of the required data tables, load driver packages in the development environment, etc., can refer to the previous article "JDBC Connection MySQL Database and example" (go to the article) the specific code is as follows: [Java]View Plaincopy
  1. Package COM.SEREIN.JDBC;
  2. Import java.sql.*;
  3. Public class Preparedstatemettest {
  4. public static void Main (string[] args) {
  5. //Check if the command line has enough 7 parameters
  6. if (args.length! = 7) {
  7. System.out.println ("Parameter error! Please Input again! ");
  8. System.exit (-1);
  9. }
  10. //program gets 7 parameter values in the run stack
  11. String name = args[0];
  12. int age = 0;
  13. try {
  14. Age = Integer.parseint (args[1]);
  15. } catch (NumberFormatException e) {
  16. System.out.println ("Parameter error! Age should is number format! ");
  17. System.exit (-1);
  18. }
  19. String sex = args[2];
  20. String address = args[3];
  21. String depart = args[4];
  22. int worklen = 0;
  23. try {
  24. Worklen = Integer.parseint (args[5]);
  25. } catch (NumberFormatException e) {
  26. System.out.println ("Parameter error! Worklen should be number format! ");
  27. System.exit (-1);
  28. }
  29. int wage = 0;
  30. try {
  31. Wage = Integer.parseint (args[6]);
  32. } catch (NumberFormatException e) {
  33. System.out.println ("Parameter error! Wage should be number format! ");
  34. System.exit (-1);
  35. }
  36. //Create PreparedStatement object
  37. PreparedStatement pstmt = null;
  38. //Create Connection object
  39. Connection conn = null;
  40. //Connect to the database and insert data
  41. try {
  42. //Load MySQL Driver instance, provides two methods, is equivalent
  43. Class.forName ("Com.mysql.jdbc.Driver");
  44. //new oracle.jdbc.driver.OracleDriver ();
  45. //Establish connection
  46. conn = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/myuser", "root", " root");
  47. //Use the PreparedStatement object to build and execute the SQL statement, 7 question marks represent the values that are pre-reserved for 7 fields
  48. pstmt = Conn.preparestatement ("INSERT into the staff (name, age, Sex,address, Depart, Worklen,wage) VALUES (?,?,?,?,?, ?, ?)");  
  49. //Use the Set method in the PreparedStatement object to set the specific value of the insertion
  50. Pstmt.setstring (1, name);
  51. Pstmt.setint (2, age);
  52. Pstmt.setstring (3, sex);
  53. Pstmt.setstring (4,address);
  54. Pstmt.setstring (5, Depart);
  55. Pstmt.setint (6, Worklen);
  56. Pstmt.setint (7, wage);
  57. Pstmt.executeupdate ();
  58. //Insert Success Prompt
  59. System.out.print ("successfully insert a data record!  ");
  60. //Capture driver Load Failure exception
  61. } catch (ClassNotFoundException e) {
  62. E.printstacktrace ();
  63. //Capture SQL statement execution failure exception
  64. } catch (SQLException e) {
  65. E.printstacktrace ();
  66. //restore variable initial value
  67. } finally {
  68. try {
  69. if (pstmt! = null) {
  70. Pstmt.close ();
  71. PSTMT = null;
  72. }
  73. if (conn! = null) {
  74. Conn.close ();
  75. conn = null;
  76. }
  77. //Catch SQL exception
  78. } catch (SQLException e) {
  79. E.printstacktrace ();
  80. }
  81. }
  82. }
  83. }
after writing the code, run the program with parameters as follows:
the input parameters are (can be referenced for modification):Sereinchan
25
M
Guangzhou
Engine
3
5000

View the console console, "Insert a data record successfully!" ":

Check the MySQL database for confirmation:

Completed successfully!

Transferred from: http://blog.csdn.net/cxwen78/article/details/6868941

JDBC Advanced PreparedStatement Execute SQL statement (MySQL)

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.