Write a code in JSP to insert a record in product table with fields: prod_d id, prod_name, quantity, price. Assuming that product table is create in MS-Access database. MCS51-june09-1b



JSP code

<%@page import="java.sql.*"%>
<%!
int prod_id=1;
String prod_name="Laptop";
int qty=2;
float price=22000.30f;
%>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:NaveenRahul");
Statement st=con.createStatement();
String query="insert into product values("+prod_id+",'"+prod_name+"',"+qty+","+price+")";
if(st.executeUpdate(query)>0){
out.write("Records inserted");
}else{
out.write("insertion faild");
}
}catch(Exception e){
out.write("Exception : "+e);
}
%>



JAVA Codes

import java.sql.*;
class Insert2{
public static void main(String ar[]){
int prod_id=2;
String prod_name="Laptop";
int qty=2;
float price=22000.30f;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:NaveenRahul");
Statement st=con.createStatement();
String query="insert into product values("+prod_id+",'"+prod_name+"',"+qty+","+price+")";
if(st.executeUpdate(query)>0){
System.out.println("Records inserted");
}else{
System.out.println("insertion faild");
}
con.close();
}catch(Exception e){
System.out.println("Exception : "+e);
}
}
}

No comments:

Popular Posts