Sie sind auf Seite 1von 4

import java.sql.

*;
import java.util.*;
class Connections
{
static final String url="jdbc:oracle:thin:@172.20.15.193:1521:orcl";
static final String user="CJET_8";
static final String pass="cmc";
Connection con=null;
public Connection myConn()
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(url,user,pass);
}
catch(Exception e)
{
System.out.println("Error"+e);
}
return con;
}
}
public class MyApplication
{
Connection con;
Statement stmt;
PreparedStatement pstmt=null;
Scanner s=null;
ResultSet rs=null;
public void dbConnection()
{
con=new Connections().myConn();
if(con.equals(null))
{
System.out.println("Null!!");
}
else
{
System.out.println("Connection successful!!");
}
}
public void createTable()
{
try{
stmt=con.createStatement();
stmt.execute("create table emplyeetable(emp_id varchar2(10),emp_fname varchar
2(20),emp_lname varchar2(30),phone_num number(10),salary number)");
System.out.println("Table is created!");
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
public void insertTable()
{
try{

s=new Scanner(System.in);
pstmt=con.prepareStatement("insert into emplyeetable values(?,?,?,?,?)");
System.out.println("Enter employee id:");
pstmt.setString(1,s.next());
System.out.println("Enter employee first name:");
pstmt.setString(2,s.next());
System.out.println("Enter employee last name:");
pstmt.setString(3,s.next());
System.out.println("Enter employee phone number:");
pstmt.setString(4,s.next());
System.out.println("Enter employee salary:");
pstmt.setString(5,s.next());
int i=pstmt.executeUpdate();
System.out.println(i+",row inserted");
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
public void updateTable()
{
try{
s=new Scanner(System.in);
pstmt=con.prepareStatement("update emplyeetable set emp_fname=?where emp_id
=?");
System.out.println("Enter new first name:");
pstmt.setString(1,s.next());
// System.out.println("Enter new last name:");
//pstmt.setString(2,s.next());
//System.out.println("Enter new phone number:");
// pstmt.setString(3,s.next());
// System.out.println("Enter new salary:");
// pstmt.setString(4,s.next());
System.out.println("Enter id of which you want to update data:");
pstmt.setString(2,s.next());
int i=pstmt.executeUpdate();
System.out.println(i+",row updated");
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
public void deleteTable()
{
try{
s=new Scanner(System.in);
pstmt=con.prepareStatement("delete from emplyeetable where emp_id=?");
System.out.println("Enter id for deletion:");
pstmt.setString(1,s.next());
int i=pstmt.executeUpdate();
System.out.println(i+",row deleted");
}
catch(Exception e)
{
System.out.println("Error"+e);
}

}
public void displayRecord()
{
try{
stmt=con.createStatement();
rs=stmt.executeQuery("select * from emplyeetable");
while(rs.next())
{
String id=rs.getString(1);
String fname=rs.getString(2);
String lname=rs.getString(3);
String phnum=rs.getString(4);
String sal=rs.getString(5);
System.out.println("Employee id: "+id+" , First Name:"+fname+" , Last Nam
e:"+lname+" , Phone Number:"+phnum+" , Salary: "+sal);
}
}
catch(Exception e)
{
System.out.println("Error"+e);
}
}
public static void main(String args[])
{
Scanner s=null;
MyApplication m=new MyApplication();
m.dbConnection();
do{
System.out.println("-----------Employee Application----------");
System.out.println("1.CREATE TABLE");
System.out.println("2.INSERT TABLE");
System.out.println("3.UPDATE TABLE");
System.out.println("4.DELETE TABLE");
System.out.println("5.DISPLAY TABLE");
System.out.println("Enter your choice:");
s=new Scanner(System.in);
int ch=s.nextInt();
switch(ch)
{
case 1:
m.createTable();
break;
case 2:
m.insertTable();
break;
case 3:
m.updateTable();
break;
case 4:
m.deleteTable();
break;
case 5:
m.displayRecord();
break;
default:
System.out.println("Enter valid option..");

s=new Scanner(System.in);
System.out.println("Do you want to continue?");
String cn=s.next();
}
while(cn.equals("Y"||cn.equals("y")))
}
}

Das könnte Ihnen auch gefallen