Public class TextOracle_AddTemplate {
Public static void main(String ags[]){
add();
}
Public static void add(){
Connection ct = null// Connection (session) with a specific database. Execute SQL statements in the connection context and return the results.
PreparedStatement ps = null// An object that represents a precompiled SQL statement.
The result set rs = null// represents the data table of the database result set, which is usually generated by executing the statement of querying the database.
Try {
//Load the driver
class . forname(" Oracle . JDBC . driver . Oracle driver ");
//Get connected
CT = drivermanager. getconnection ("JDBC: Oracle: thin: @192.168.1.100:1521:allence". //"jdbc:oracle:thin:@ip address: port number: database name ","user name "password"
//Create a PreparedStatement interface reference object.
Ps = CT. PrepareStatement ("Insert into EMP (Empno, ename)values(8888,' Xiaoming')"); //Fill in the SQL statement
system . out . println(PS);
//Complete the query
PS . execute update();
}catch (exception e){
e . printstacktrace(); //Print abnormal information
Throw a new RuntimeException (e.getMessage ()); //Throws an exception
} Finally {
If (rs! =null){
Try {
RS . close();
}catch(SQLException e){
e . printstacktrace();
}
rs = null
}
If (ps! =null){
If (rs! =null){
Try {
PS . close();
}catch(SQLException e){
e . printstacktrace();
}
ps = null
}
If (ct! =null){
Try {
CT . close();
}catch(SQLException e){
e . printstacktrace();
}
ps = null
}
}
}
}
Give you a template directly and control adding, deleting and modifying. In fact, you can write the methods of adding, deleting, modifying and querying into one class, and then use another class to control the whole program. If you don't do this, it may have a certain impact on the whole program when adding functions in the future.