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
PreparedStatement
Object. 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
- Package COM.SEREIN.JDBC;
- Import java.sql.*;
- Public class Preparedstatemettest {
- public static void Main (string[] args) {
- //Check if the command line has enough 7 parameters
- if (args.length! = 7) {
- System.out.println ("Parameter error! Please Input again! ");
- System.exit (-1);
- }
- //program gets 7 parameter values in the run stack
- String name = args[0];
- int age = 0;
- try {
- Age = Integer.parseint (args[1]);
- } catch (NumberFormatException e) {
- System.out.println ("Parameter error! Age should is number format! ");
- System.exit (-1);
- }
- String sex = args[2];
- String address = args[3];
- String depart = args[4];
- int worklen = 0;
- try {
- Worklen = Integer.parseint (args[5]);
- } catch (NumberFormatException e) {
- System.out.println ("Parameter error! Worklen should be number format! ");
- System.exit (-1);
- }
- int wage = 0;
- try {
- Wage = Integer.parseint (args[6]);
- } catch (NumberFormatException e) {
- System.out.println ("Parameter error! Wage should be number format! ");
- System.exit (-1);
- }
- //Create PreparedStatement object
- PreparedStatement pstmt = null;
- //Create Connection object
- Connection conn = null;
- //Connect to the database and insert data
- try {
- //Load MySQL Driver instance, provides two methods, is equivalent
- Class.forName ("Com.mysql.jdbc.Driver");
- //new oracle.jdbc.driver.OracleDriver ();
- //Establish connection
- conn = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/myuser", "root", " root");
- //Use the PreparedStatement object to build and execute the SQL statement, 7 question marks represent the values that are pre-reserved for 7 fields
- pstmt = Conn.preparestatement ("INSERT into the staff (name, age, Sex,address, Depart, Worklen,wage) VALUES (?,?,?,?,?, ?, ?)");
- //Use the Set method in the PreparedStatement object to set the specific value of the insertion
- Pstmt.setstring (1, name);
- Pstmt.setint (2, age);
- Pstmt.setstring (3, sex);
- Pstmt.setstring (4,address);
- Pstmt.setstring (5, Depart);
- Pstmt.setint (6, Worklen);
- Pstmt.setint (7, wage);
- Pstmt.executeupdate ();
- //Insert Success Prompt
- System.out.print ("successfully insert a data record! ");
- //Capture driver Load Failure exception
- } catch (ClassNotFoundException e) {
- E.printstacktrace ();
- //Capture SQL statement execution failure exception
- } catch (SQLException e) {
- E.printstacktrace ();
- //restore variable initial value
- } finally {
- try {
- if (pstmt! = null) {
- Pstmt.close ();
- PSTMT = null;
- }
- if (conn! = null) {
- Conn.close ();
- conn = null;
- }
- //Catch SQL exception
- } catch (SQLException e) {
- E.printstacktrace ();
- }
- }
- }
- }
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)