import java.sql.*;
public class jdjd
{
static BufferedReader br;
Connection conn;
Statement stmt;
jdjd () throws Exception
{
String driver=”sun.jdbc.odbc.JdbcOdbcDriver”;
String url=”jdbc:odbc:dbase”;
Class.forName(driver);
conn=DriverManager.getConnection(url);
stmt=conn.createStatement();
}
void addrec() throws Exception
{
String s1,s2,sql;
System.out.println(“Enter Roll No. : “);
s1=br.readLine();
System.out.println(“Enter Name : “);
s2=br.readLine();
sql=”insert into student values(” + s1 + “, ‘” + s2 + “‘)”;
stmt.executeUpdate(sql);
}
void viewrec() throws Exception
{
int i,cols;
String sql=”select * from student”;
ResultSet rs=stmt.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData();
cols=rsmd.getColumnCount();
for(i=1;i<=cols;i++)
{
System.out.println(” ” + rsmd.getColumnClassName(i));
}
while(rs.next())
{
System.out.println();
for(i=1;i<=cols;i++)
{
System.out.println(” ” + rs.getString(i));
}
}
}
void search() throws Exception
{
int i,cols;
String s1;
System.out.println(“Enter Roll No. : “);
s1=br.readLine();
String sql=” select name from student where rno=”+s1+” “;
ResultSet rs=stmt.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData();
cols=rsmd.getColumnCount();
for(i=1;i<=cols;i++)
{
System.out.println(” ” + rsmd.getColumnClassName(i));
}
while(rs.next())
{
System.out.println();
for(i=1;i<=cols;i++)
{
System.out.println(” ” + rs.getString(i));
}
}
}
void update() throws Exception
{
int i,cols;
String s1,s2;
System.out.println(“Enter Roll No. to Update: “);
s1=br.readLine();
System.out.println(“Enter New Name : “);
s2=br.readLine();
String sql=” update student set name = ‘” + s2 + “‘ where rno=” + s1 + ” “;
stmt.executeUpdate(sql);
}
void delrec() throws Exception
{
String s1,s2,sql;
System.out.println(“Enter Roll No. : “);
s1=br.readLine();
sql=”delete from student where rno=”+s1;
stmt.executeUpdate(sql);
}
void closeAll() throws Exception
{
stmt.close();
conn.close();
}
public static void main (String args[])
{
try
{
br=new BufferedReader(new InputStreamReader(System.in));
String temp;
jdjd d=new jdjd();
do
{
System.out.println(“1.Add Record”);
System.out.println(“2.View Record”);
System.out.println(“3.Delete Record”);
System.out.println(“4.Search Record”);
System.out.println(“5.Update Record”);
System.out.println(“5.Exit”);
System.out.println(“Enter UR Choice”);
temp=br.readLine();
if(temp.equals(“1”))
{
d.addrec();
}
else if(temp.equals(“2”))
{
d.viewrec();
}
else if(temp.equals(“3”))
{
d.delrec();
}
else if(temp.equals(“4”))
{
d.search();
}
else if(temp.equals(“5”))
{
d.update();
}
}while(!temp.equals(“6”));
d.closeAll();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Leave a Reply