Databse Connectivity in Java

3 comments - Post a comment

Program in java for inserting, recording, deleting, editing and searching student details stored in the SQL database.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;


class studentdetails
{
Statement stmt;
String strSql;
Connection con;
BufferedReader bufferObj;
studentdetails (){
bufferObj=new BufferedReader(new InputStreamReader(System.in));
}

void establishConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex)
{
System.out.println(ex);
}

try
{
String url="jdbc:odbc:new_dsn";
con=DriverManager.getConnection(url,"sa","");
stmt=con.createStatement();
}
catch(SQLException ce)
{
System.out.println("Error... "+ce);
}
}


void add()
{
try
{
System.out.println("\nEnter Roll Number: ");
String rollno=bufferObj.readLine();

System.out.println("\nEnter Students Name: ");
String name=bufferObj.readLine();

System.out.println("\nEnter Course Name");
String course=bufferObj.readLine();

strSql="Insert into Student values(" +rollno+",'"+name+"',' "+course+" ')";

stmt.executeUpdate(strSql);
System.out.println("\nRecords Successfully Added!");

System.out.println();
}

catch(SQLException ce)
{
System.out.println("Error... "+ce);
}

catch(Exception e)
{
System.out.println(e);
}
}

void update()
{
try
{
System.out.println("\nEnter Roll Number whose "+"record should be updated: ");
String roll=bufferObj.readLine();
System.out.println("Enter Students Name to be Modified: ");
String name=bufferObj.readLine();
System.out.println("Enter course name to be modified: ");
String course=bufferObj.readLine();
strSql="update Student set name=' "+name+" ',course='"+course+"' where rollno='"+roll+"'";

stmt.executeUpdate(strSql);
System.out.println("\nRecord Successfully Modified !");
System.out.println();
}

catch(SQLException ioe)
{
System.out.println(ioe);
}

catch(Exception er)
{
System.out.println(er);
}
}

void delete()
{
try
{

System.out.println("Enter Students Name to be Deleted :") ;
String name=bufferObj.readLine();
strSql="Delete from Student where rtrim(name) like '"+name+ "'";

stmt.executeUpdate(strSql);
System.out.println("Record Successfully Deleted !");
System.out.println();
}

catch(SQLException et)
{
System.out.println("Error in Deletion.... "+et);
}

catch(Exception ty)
{
System.out.println("Error.... "+ty);
}
}

void search()
{
try
{
System.out.println("Enter Roll Number "+"whose record should be searched: ");
int roll=Integer.parseInt(bufferObj.readLine());

System.out.println("Enter Students Name to be Searched");
String name=bufferObj.readLine();

strSql="select * from student where name like'"+name;
strSql=strSql+"' and Rollno="+roll;

ResultSet rs=stmt.executeQuery(strSql);
if(!rs.next())
{
System.out.println("Name");
System.out.println(rs.getString(1)+"\t");
System.out.println("Roll Number");
System.out.println(rs.getInt(2)+"\t");
System.out.println("Course");
System.out.println(rs.getString(3)+"\n");
}
}

catch(SQLException er)
{
System.out.println(er);
}

catch(Exception t)
{
System.out.println(t);
}
}

public void menudisplay() throws IOException
{
char choice;
while(true)
{
System.out.println();
System.out.println("1. Add a Record");
System.out.println("2. Modify Record");
System.out.println("3. Delete a Record");
System.out.println("4. Search a Record");
System.out.println("5. Exit");
System.out.println("Enter your choice....:");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
choice=(char) br.read();
switch(choice)
{
case '1' :
System.out.println("Adding a Record........");
add();
break;

case '2' :
System.out.println("Updating a Record........");
update();
break;

case '3' :
System.out.println("Deleting a Record........");
delete();
break;

case '4' :
System.out.println("Searching a Record........");
search();
break;

case '5' :
System.exit(0);
break;

default :
System.out.println("Adding a Record........");
add();
break;

}
}
}
}

class student
{
public static void main(String[] args) throws IOException
{
studentdetails stud = new studentdetails();
stud.establishConnection();
stud.menudisplay();
}
}

 
This Post has 3 Comments Add your own!
Anonymous - June 27, 2008 at 12:00 PM

Its Was Useful for me.
Thank You

mehernosh - July 15, 2008 at 9:55 PM

hey i'm swingin a P4 with 256MB of ram and a vga card 16MB
u have any idea how i can get a 3D api to work on my box?
any samples will b appreciated


mehernosh47@gmail...

Anonymous - December 26, 2010 at 6:44 PM

hello there shows ssome error in the program "Databse Connectivity in Java" like error in mysql exception what i have to do please explain it

Post a Comment