Sunday, May 9, 2010

programs

Menu.java


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

package javaapplication1;

/**
*
* @author Sravanthi
*/
import java.util.*;
public class Menu
{
Scanner sc=new Scanner(System.in);
int no=0;
String name=null;
public Menu()
{}
public void mainmenu()
{
int mchoice=0;
System.out.println("***Main Menu***");
System.out.println("----------------");
System.out.println("1.Admin");
System.out.println("2.User");
System.out.print("enter your choice:");
mchoice=sc.nextInt();
switch(mchoice)
{
case 1:adminmenu();
break;
case 2:usermenu();
break;
}
}

public void adminmenu()
{
do
{
int achoice=0;

System.out.println("****Admin****");
System.out.println("--------------");
System.out.println("1.View");
System.out.println("2.Insert");
System.out.println("3.Update");
System.out.println("4.Delete");
System.out.println("5.Main menu");
System.out.println("6.Exit");
System.out.print("enter your choice:");
achoice=sc.nextInt();
switch(achoice)
{
case 1:
View.view();
break;
case 2:System.out.println("Enter the no:");
no=sc.nextInt();
System.out.println("Enter the name:");
name=sc.next();
Insert.insert(no,name);
break;
case 3:Update.update();
break;
case 4:Delete.delete();
break;
case 5:mainmenu();
break;
case 6:System.exit(0);
default:System.out.println("invalid choice");
}
}while(true);
}

public void usermenu()
{
do
{
int uchoice=0;

System.out.println("****Admin****");
System.out.println("--------------");
System.out.println("1.View");
System.out.println("2.Main menu");
System.out.println("3.Exit");
System.out.print("enter your choice:");
uchoice=sc.nextInt();
switch(uchoice)
{
case 1:View.view();
break;

case 2:mainmenu();
break;
case 3:System.exit(0);
default:System.out.println("invalid choice");
}
}while(true);
}

public static void main(String args[])
{
Menu m=new Menu();
m.mainmenu();
}
}





MySqlconnectin.java



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

package javaapplication1;

/**
*
* @author Sravanthi
*/
import java.sql.*;
public class MysqlConnection
{
public static Connection getConnection()
{
Connection con=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");

}
catch(ClassNotFoundException ce)
{
ce.printStackTrace();
}
catch(SQLException sq)
{
sq.printStackTrace();
}
return con;
}
}



insert.java

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

package javaapplication1;

/**
*
* @author Sravanthi
*/
import java.sql.*;
public class Insert
{
public static void insert(int no,String name)
{
try
{
String sql = "insert into first values("+no+",'"+name+"') ";
Connection connection = MysqlConnection.getConnection();
Statement statement = connection.createStatement();
int n=statement.executeUpdate(sql);
if(n>0)
System.out.println("Record inserted successfully");
else
System.out.println("Record was not inserted");

}
catch(SQLException sq)
{
sq.printStackTrace();
}

}


}


Delete.java

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

package javaapplication1;

/**
*
* @author Sravanthi
*/
import java.sql.*;
public class Delete
{
public static void delete()
{
try
{
String sql = "delete from first where id=1 ";
Connection connection = MysqlConnection.getConnection();
Statement statement = connection.createStatement();
int n=statement.executeUpdate(sql);
if(n>0)
System.out.println("Record was successfully deleted");
else
System.out.println("Record was not deleted");

}
catch(SQLException sq)
{
sq.printStackTrace();
}

}
public static void main(String args[])
{

Delete.delete();
}
}


update.java

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

package javaapplication1;

/**
*
* @author Sravanthi
*/
import java.sql.*;
public class Update
{
public static void update()
{
try
{
String sql = "update first set id=121 where id=2 ";
Connection connection = MysqlConnection.getConnection();
Statement statement = connection.createStatement();
int n=statement.executeUpdate(sql);
if(n>0)
System.out.println("Record was successfully updated");
else
System.out.println("Record was not updated");

}
catch(SQLException sq)
{
sq.printStackTrace();
}

}
public static void main(String args[])
{

Update.update();
}
}


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

package javaapplication1;

/**
*
* @author Sravanthi
*/
import java.sql.*;
public class View
{
public static void view()
{
try
{
String sql = "select * from first ";
Connection connection = MysqlConnection.getConnection();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getInt(1)+"\t"+rs.getString(2));
}
}
catch(SQLException sq)
{
sq.printStackTrace();
}

}


}

No comments:

Post a Comment