Sunday, January 30, 2011

How Insert, Update and Delete in Java (Oracle Database)

After we know about select in database the next chapter is how to insert,update and delete data at oracle database using java programming language. First open your IDE for this case i will use Netbeans 6.8.Please attention in code bellow:

ConnectionDatabase.java


import java.sql.*;
public class ConnectionDatabase {
Connection conn=null;

public Connection getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String _user="dbContact";
String _pass="contact12345";
conn=DriverManager.getConnection(url,_user,_pass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch ( SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main (String[] args) {
new ConnectionDatabase().getConnection();

}
}

InsertDatabase.java

import java.util.Scanner;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class InsertDatabase {

public ConnectionDatabase conn = new ConnectionDatabase();
public Connection conx = null;
public int _success = 0;

public InsertDatabase() {
String _id = "", _name = "", _address = "";
Scanner sc = new Scanner(System.in);
System.out.println("Insert Data");
System.out.println("------------------------");
System.out.print("ID\t: ");
_id = sc.next();
System.out.print("Name\t: ");
_name = sc.next();
System.out.print("Address\t: ");
_address = sc.next();
insertData(_id, _name, _address);
}

public void insertData(String _id, String _name, String _address) {
//Query syntax
try {
String sql = "Insert into Contact values(?,?,?)";
conx = conn.getConnection();
PreparedStatement stat = conx.prepareCall(sql);
stat.setString(1, _id);
stat.setString(2, _name);
stat.setString(3, _address);
_success = stat.executeUpdate();
if (_success == 1) {
System.out.println("Data successfully insert");
}
} catch (SQLException ex) {
Logger.getLogger(InsertDatabase.class.getName()).log(Level.SEVERE, null, ex);
}
}

public static void main(String[] args) {
new InsertDatabase();
}
}


Note: Don't use space in insert data.

Result:


UpdateDatabase.java

import java.sql.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UpdateDatabase {

public ConnectionDatabase conn = new ConnectionDatabase();
public Connection conx = null;
public int _success = 0;

public UpdateDatabase() {
String _id = "", _name = "", _address = "";
Scanner sc = new Scanner(System.in);
System.out.println("Update Data");
System.out.println("------------------------");
System.out.print("Update ID[Enter ID you want delete]\t: ");
_id = sc.next();
System.out.print("Name\t: ");
_name = sc.next();
System.out.print("Address\t: ");
_address = sc.next();
updateData(_id, _name, _address);
}

public void updateData(String _id, String _name, String _address) {
//Query syntax
try {
String sql = "Update Contact set Name=?,Address=? where ID=?";
conx = conn.getConnection();
PreparedStatement stat = conx.prepareCall(sql);
stat.setString(1, _name);
stat.setString(2, _address);
stat.setString(3, _id);
_success = stat.executeUpdate();
if (_success == 1) {
System.out.println("Data successfully update");
} else {
System.out.println("Data failed update");
}
} catch (SQLException ex) {
Logger.getLogger(UpdateDatabase.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
new UpdateDatabase();
}
}


Note: Don't use space in insert data.

Result:(Update name and adress in id 2(Before:Tommy Oxford. After Tana Captain))


Before Update

After Update


DeleteDatabase.java

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DeleteDatabase {

public ConnectionDatabase conn = new ConnectionDatabase();
public Connection conx = null;
public int _success = 0;

public DeleteDatabase() {
String _id = "";
Scanner sc = new Scanner(System.in);
System.out.println("Delete Data");
System.out.println("------------------------");
System.out.print("Delete ID: ");
_id = sc.next();
deleteData(_id);
}

public void deleteData(String _id) {
//Query syntax
try {
String sql = "Delete from Contact where ID=?";
conx = conn.getConnection();
PreparedStatement stat = conx.prepareCall(sql);
stat.setString(1, _id);
_success = stat.executeUpdate();
if (_success == 1) {
System.out.println("Data successfully delete");
} else {
System.out.println("Data failed deleted");
}
} catch (SQLException ex) {
Logger.getLogger(UpdateDatabase.class.getName()).log(Level.SEVERE, null, ex);
}
}

public static void main(String[] args) {
new DeleteDatabase();
}
}

Result:

Okay that's is it and finally congratulation you have know how to insert,update,and delete data in java. I hope with this tutorial can help you...Thank You...^^

No comments:

Post a Comment