Friday, May 7, 2010

course program

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package preparetes;

/**
*
* @author admin
*/
import java.util.*;
import java.sql.*;
public class Course {
Scanner sc=new Scanner(System.in);
Connection con=null;
Statement st=null;
ResultSet rs=null;

public void connection()
{


try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/s","root","admin");
st=con.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public Course()
{
int choice;
choice=menu();
switch(choice)
{
case 1:adminmenu();
break;
case 2:usermenu();
break;
}
}

public int menu()
{
int mchoice=0;

System.out.println("Main Menu");
System.out.println("---------------------");
System.out.println("1.Admin");
System.out.println("2.User");
System.out.println("-------------------------");
System.out.print("enter your choice:");
mchoice=sc.nextInt();
return mchoice;


}
public static void main(String args[])
{
Course c=new Course();
}

public int adminmenu()
{
int schoice=0;
System.out.println("Admin menu");
System.out.println("-----------");
System.out.println("1.View");
System.out.println("2.Insert a record");
System.out.println("3.Update a record");
System.out.println("4.Delete a record");
System.out.println("5.Return to the main menu");
System.out.println("6.Exit");
System.out.println("-----------------------------");
System.out.print("enter your choice:");
schoice=sc.nextInt();
switch(schoice)
{
case 1:view();
break;
case 2:insert();
break;
case 3:update();
break;
case 4:delete();
break;
case 5:menu();
break;
case 6:System.exit(0);
}
return schoice;
}

public void usermenu() {
int uchoice=0;
System.out.println("User menu");
System.out.println("----------");
System.out.println("1.View");
System.out.println("2.Return to the main menu");
System.out.println("3.Exit");
System.out.println("--------------------");
System.out.print("enter your choice:");
uchoice=sc.nextInt();
switch(uchoice)
{
case 1:view();
break;
case 2:menu();
break;
case 3:System.exit(0);
}
}


private void delete() {
try
{
view();
System.out.println("enter the record no to be deleted:");
int n=st.executeUpdate("delete from first where no=1");
if(n>0)
System.out.println("Record is successfully deleted");
}
catch(Exception e)
{
e.printStackTrace();
}
}

private void insert() {
try
{
int n=st.executeUpdate("insert into first values(7,'Temporary')");
if(n>0)
System.out.println("Record is successfully inserted");
}
catch(Exception e)
{
e.printStackTrace();
}
}

private void update()
{
try
{
view();
System.out.print("Enter the new name:");
int n=st.executeUpdate("update first set name='c' where no=1");
if(n>0)
System.out.println("Record is successfully updated");
}
catch(Exception e)
{
e.printStackTrace();
}
}

private int view()
{
try
{
rs=st.executeQuery("select * from first");
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+rs.getString(2));
}
}
catch(Exception e)
{
e.printStackTrace();
}
return 0;
}
}

No comments:

Post a Comment