Create Test table
Java projects in eclipse
Code
Database Method Class Dbutil:
Package Util;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;public class Dbutil {//Create a database connection public static Connection Getconnecti On () {Connection Connection = null; String USERNAMR = "System"; String PASSWORD = "* * * *";//own password string drvier = "Oracle.jdbc.OracleDriver"; String URL = "Jdbc:oracle:thin: @localhost: 1521:ORCL"; try {class.forname (drvier); Connection = Drivermanager.getconnection (URL, USERNAMR, PASSWORD); SYSTEM.OUT.PRINTLN ("Database successfully connected"); } catch (ClassNotFoundException e) {throw new RuntimeException (e); } catch (SQLException e) {throw new RuntimeException (e); } return connection; }//close resource public static void Close (Connection Connection) {try {if (Connection! = null) {Connection.close ();}} catch (SQLException e) {E.printstacktrace ();}}public static void Close (PreparedStatement preparedstatement) {try {if (PreparedStatement! = null) {preparedstatement.cl OSE ();}} catch (SQLException e) {e.printstacktrace ();}} public static void Close (ResultSet ResultSet) {try {if (ResultSet! = null) {Resultset.close ()}} catch (SQLException e) { E.printstacktrace ();}}}
Entity class Model
Packagetest; Public classModel {PrivateString ID; PrivateString username; PrivateString password; PrivateString name; Private intAge ; PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } PublicModel () {} PublicModel (string ID, string username, string password, string name,intAge ) { Super(); This. ID =ID; This. Username =username; This. Password =password; This. Name =name; This. Age =Age ; } Public voidUpdate (string username, string password, string name,intAge ) { This. Username =username; This. Password =password; This. Name =name; This. Age =Age ; } PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }}
Testing class Test
Packagetest;Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importutil. Dbutil; Public classTest { Public voidAdd (model Model)//Add Data{Connection Connection=dbutil.getconnection (); String SQL= "INSERT into Test (id,username,password,name,age) VALUES (?,?,?,?,?)"; PreparedStatement PreparedStatement=NULL; Try{PreparedStatement=connection.preparestatement (SQL); Preparedstatement.setstring (1, Model.getid ()); Preparedstatement.setstring (2, Model.getusername ()); Preparedstatement.setstring (3, Model.getpassword ()); Preparedstatement.setstring (4, Model.getname ()); Preparedstatement.setint (5, Model.getage ()); Preparedstatement.executeupdate (); System.out.println ("Insert Succeeded"); } Catch(SQLException e) {//TODO Auto-generated catch blockE.printstacktrace (); }finally{dbutil.close (PreparedStatement); Dbutil.close (connection); } } Public voidUpdate (model model)//Modifying Data{Connection Connection=dbutil.getconnection (); String SQL= "Update test set username=?,password=?,name=?,age=?"; PreparedStatement PreparedStatement=NULL; Try{PreparedStatement=connection.preparestatement (SQL); Preparedstatement.setstring (1, Model.getusername ()); Preparedstatement.setstring (2, Model.getpassword ()); Preparedstatement.setstring (3, Model.getname ()); Preparedstatement.setint (4, Model.getage ()); Preparedstatement.executeupdate (); } Catch(SQLException e) {//TODO Auto-generated catch blockE.printstacktrace (); }finally{dbutil.close (PreparedStatement); Dbutil.close (connection); } } PublicModel load (String ID)//Querying Data{Connection Connection=dbutil.getconnection (); //Preparing SQL statementsString sql = "SELECT * from test where id =?"; //To create a statement transfer objectPreparedStatement PreparedStatement =NULL; ResultSet ResultSet=NULL; Model Model=NULL; Try{PreparedStatement=connection.preparestatement (SQL); Preparedstatement.setstring (1, id); ResultSet=Preparedstatement.executequery (); while(Resultset.next ()) {model=NewModel (); Model.setid (ID); Model.setusername (Resultset.getstring ("Username")); Model.setpassword (Resultset.getstring ("Password")); Model.setname (Resultset.getstring ("Name")); Model.setage (Resultset.getint ("Age")); } } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{dbutil.close (resultSet); Dbutil.close (PreparedStatement); Dbutil.close (connection); } returnmodel; } Public voidDelete (String ID)//Delete Data{Connection Connection=dbutil.getconnection (); String SQL= "Delete from test where id=?"; PreparedStatement PreparedStatement=NULL; Try{PreparedStatement=connection.preparestatement (SQL); Preparedstatement.setstring (1, id); Preparedstatement.executequery (); System.out.println ("Delete Succeeded"); } Catch(SQLException e) {//TODO Auto-generated catch blockE.printstacktrace (); }finally{dbutil.close (PreparedStatement); Dbutil.close (connection); } } Public Static voidMain (String args[]) {test test=NewTest (); Model Model=NewModel ("1", "123", "123", "Zhang San", 18);//Test.add (model); //model.update ("123", "123", "Zhang San",);//test.update (model);// //model=test.load ("1");//System.out.println ("Query results ———— Name:" +model.getname () + ", Age:" +model.getage ());//Test.delete ("1"); }}
Connect to the Oracle database with Navicat because the tables created are automatically stored in the system table space and can be found directly below the system.
Data changes in Navicat:
Insert data.
The data after the change.
Delete the data.
Java Connection Oracle Database implementation additions and deletions and displays in Navicat