The following is the JAVA source code for the Java Database Connectivity .

The program does the following functions

1.View Records

2.Update Records

3.Insert Records

4.Delete Records

 

//Importing the java packages

import java.io.*;
import java.sql.*;
import java.util.*;

//This is the main class

public class JDBCAccess
{

public static void main(String[] args){

try{
Connection con = getConnection();
con.setAutoCommit(false);
Statement stm = con.createStatement();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;


int ch;

System.out.println("Enter 1 to retrieve the detials");
System.out.println(" 2 to update database");
System.out.println(" 3 to insert into database");
System.out.println(" 4 to delete from the database");

line = br.readLine();
ch=Integer.parseInt(line); 
System.out.println(" The choice is"+ch);
switch(ch)
{
case 1:getTableInfo("userdb",stm);
stm.close();
break;

case 2:System.out.println("Iam here");
update1("userdb",stm);
con.commit();
stm.close();
break;

case 3:insert("userdb",stm);
con.commit();
stm.close();

break;

case 4:delete("userdb",stm);
con.commit();
stm.close();
break;

default:break; 
}

}

catch(SQLException e){
System.out.println("SQLException:");
System.out.println("Message: " + e.getMessage());
}

catch(IOException e){
System.out.println("IOException:");
System.out.println("Message: " + e.getMessage());
}

catch(ClassNotFoundException e){
System.out.println("IOException:");
System.out.println("Message: " + e.getMessage());
}

}

//This function does the connection part with the database 
public static Connection getConnection()throws SQLException, IOException, ClassNotFoundException

{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:userdb";
String username ="";
String password ="";
Enumeration e = DriverManager.getDrivers();
return DriverManager.getConnection(url,username,password);
}

//This function does the retrieving of the data

public static void getTableInfo(String table, Statement stm)throws SQLException,IOException
{
/*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
int ch;
System.out.println("Enter the ID");
line = br.readLine();
ch=Integer.parseInt(line);*/
String qry = "Select * From "+ table;
ResultSet rs = stm.executeQuery(qry);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
while (rs.next())
{
System.out.println();

for (int i=1;i<=count; i++)
{
System.out.println(rs.getString(i));
}
}
System.out.println();
rs.close();
}

//This function does the updating part
public static void update1(String userdb, Statement stmt)throws SQLException,IOException
{int ch;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line,temp1,temp2,temp3;
System.out.println("Enter the ID");
line = br.readLine();
ch=Integer.parseInt(line);
System.out.println("Please Enter the First Name");
temp1 = new String(br.readLine());
System.out.println("Please Enter the Last Name");
temp2 = new String(br.readLine( ));
System.out.println("Please Enter the Password");
temp2 = new String(br.readLine());

String sqlString = new String("UPDATE userdb SET FirstName=temp1,LastName=temp2,Password=temp3 WHERE ID=ch ");
stmt.executeUpdate(sqlString); // Execute the update statement
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}

 

//This function does the inserting part

public static void insert(String userdb, Statement stmt)throws SQLException,IOException
{
int ch;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line,temp1,temp2,temp3;
System.out.println("Please Enter the ID ");
line = br.readLine();
ch=Integer.parseInt(line);
System.out.println("Please Enter the First Name");
temp1 = new String(br.readLine());
System.out.println("Please Enter the Last Name");
temp2 = new String(br.readLine());
System.out.println("Please Enter the Password");
temp2 = new String(br.readLine());

String sqlString = new String("INSERT INTO userdb (ID,FirstName,LastName,Password) VALUES (ch,temp1, temp2,temp3) ");
stmt.executeUpdate(sqlString); // Execute the insert statement

catch (SQLException e) {
System.out.println(e.getMessage());
}
}

//This function does the deletion part
public static void delete(String userdb, Statement stmt)throws SQLException,IOException
{
int ch; 
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
System.out.println("Enter the ID");
line = br.readLine();
ch=Integer.parseInt(line);

String sqlString = new String("DELETE FROM userdb WHERE ID=ch");
stmt.executeUpdate(sqlString); // Execute the delete statement
} catch (SQLException e) {
System.out.println(e.getMessage());
}

}



}