Placeholder (?) when using PreparedStatement to execute an SQL statement (?) Usage, preparedstatement
1. Student database table
2. Java code
Public static void main (String [] args ){
Int _ id = 1;
String _ name = "zhangsan ";
String _ gender = "male ";
Connection con = null;
PreparedStatement ps = null;
Try {
// Load the driver
Class. forName ("com. mysql. jdbc. Driver ");
// Use the driver to create a connection
Con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/mysql", "root", "111111 ");
// Define an SQL statement
String SQL = "insert into hehe values (?,?,?) ";
// Create an execution command object
Ps = con. prepareStatement (SQL );
// Set parameters
Ps. setInt (1, 1 );
Ps. setString (2, _ name );
Ps. setString (3, _ gender );
// Execute the command and accept the result
Int result=ps.exe cuteUpdate ();
System. out. println (result );
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Try {
If (null! = Ps)
Ps. close ();
If (null! = Con)
Con. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
}
3. The result is displayed.
ID |
Name |
Gender |
1 |
Zhang San |
Male |