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();
}

}


}

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;
}
}

Wednesday, May 5, 2010

getObject() example

package com.stardeveloper.servlets.db;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InsertServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.print("");

out.print("
out.print( req.getRequestURI() );
out.print("\" method=\"post\">");
out.print("First Name :
");
out.print("
");
out.print("Last Name :
");
out.print("");
out.print("

");
out.print(" Insert Record");
out.print(" ");
out.print(" Display Records
");

out.print("");

out.close();
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

res.setContentType("text/html");
PrintWriter out = res.getWriter();

out.print("");

out.print("
");

out.println("ID\tFirst Name\tLast Name\n");

// receiving parameters

String first = req.getParameter("first").trim();
String last = req.getParameter("last").trim();
boolean proceed = false;

if(first != null && last != null)
if(first.length() > 0 && last.length() > 0)
proceed = true;

// connecting to database

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement ps = null;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:odbc_exmp");

String sql;
sql = "INSERT INTO Names(first_name, last_name) VALUES (?,?)";
ps = con.prepareStatement(sql);
stmt = con.createStatement();

// inserting records

if(proceed) {
ps.setString(1, first);
ps.setString(2, last);
ps.executeUpdate();
}

// displaying records

rs = stmt.executeQuery("SELECT * FROM Names");
while(rs.next()) {
out.print(rs.getObject(1).toString());
out.print("\t");
out.print(rs.getObject(2).toString());
out.print("\t\t");
out.print(rs.getObject(3).toString());
out.print("\n");
}


} catch (SQLException e) {
throw new ServletException(e);
} catch (ClassNotFoundException e) {
throw new ServletException(e);
} finally {
try {
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(ps != null)
ps.close();
if(con != null)
con.close();
} catch (SQLException e) {}
}

out.print("");
out.close();
}
}