Connectionconthis. creatCon (); get the connection PreparedStatementpstmnull; command Stringsqlquot; insertintostudent (stu
Connection con = this. creatCon (); // get the Connection PreparedStatement pstm = null; // command String SQL = quot; insert into student (stu
Connection con = this. creatCon (); // get the Connection
PreparedStatement pstm = null; // command
String SQL = "insert into student (student_id, name, sex, age) values (?,?,?,?) "; // Set the SQL statement to be executed
Pstm = con. prepareStatement (SQL); // prepare to execute the SQL statement command
Pstm. setString (2, name); // set the value of the 2nd question mark
Pstm. setString (3, sex); // set the value of 3rd question marks
Pstm. setInt (4, age); // set the value of 4th question marks
Pstm. setInt (1, student_id); // set the value of 1st question marks
// Pcmd.exe cuteQuery (); // execute the SQL command. This command returns the ResultSet
Pcmd.exe cuteUpdate (); // run the SQL command to insert, delete, and modify data.
System. out. println ("Write successful"); // whether the write is successful after execution
Connection opening and closing:
// Create a class to connect to the Oracle database
Public static Connection creatCon ()
{
String username = "scott ";
String password = "tiger ";
Connection con = null;
Try
{
Class. forName ("oracle. jdbc. driver. OracleDriver ");
String url = "java: oracle: thin: @ localhost: 1521: ora9i"; // connect to the Oracle database
/* Class. forName ("com. micrsoft. jdbc. sqlserver. SQLServerDriver ");
String url = "java: microsoft: sqlserver: // localhost: 1433"; // connect to the SQL server database */
Con = (Connection) DriverManager. getConnection (url, username, password );
Return con;
}
Catch (Exception exc)
{
System. out. println (exc );
}
Return null;
}
// Close the connection
Public void closeCon (Connection c)
{
Try
{
C. close ();
}
Catch (Exception c1)
{
System. out. println (c1 );
}
}