Answer Posted / rohil
JDBC IS JAVA DATABASE CONNECTIVITY
this is used to get our java code connected with database
so that everytime we execute our code we dont have to start
from beginnning ...ie again making objects and giving
values again...with databse connectivty u can store the
data once and for all...
package com.mypack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import oracle.jdbc.driver.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet Class
*
* @web.servlet name="CDataAccess"
* display-name="Name for
CDataAccess"
* description="Description for
CDataAccess"
* @web.servlet-mapping url-pattern="/CDataAccess"
* @web.servlet-init-param name="A parameter"
* value="A value"
*/
public class CDataAccess extends HttpServlet {
Connection con=null;
Statement st=null;
PrintWriter out=null;
ResultSet rs=null;
public CDataAccess() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
IOException {
// TODO Auto-generated method stub
//super.doGet(req, resp);
out=resp.getWriter();
String prodId=(String)req.getParameter
("txtProdId");
String prodName=(String)req.getParameter
("txtProdName");
try
{
//register the driver with the
application
DriverManager.registerDriver(new
oracle.jdbc.OracleDriver());
//connecting with the database
con=DriverManager.getConnection
("jdbc:oracle:thin:databse name:1521:javadb","name of
server","password");
//Connection c=getCon();
if(!con.isClosed() )
{
out.println("Connection
established");
st=con.createStatement();//creates
an object of statement
String query="insert into Products
values('"+prodId+"','"+prodName+"')";
int i=st.executeUpdate(query);
if(i>0)
{
out.println("Data
inserted"+"<br/>");
}
String q="select * from Products";
rs=st.executeQuery(q);
while(rs.next())
{
out.println(rs.getString
("PID")+","+rs.getString("PNAME")+"<br/>");
}
st.close();
}
}
catch(SQLException ex){
out.println(ex.getMessage());
}
}
| Is This Answer Correct ? | 0 Yes | 3 No |
Post New Answer View All Answers
What happens when the parent process of a child process exits before the child ?
What is data encapsulation?
How should I format my code? How should I comment my code?
What is predicate in lambda expression?
How do you check if java is installed on windows command prompt?
What is an api in java?
What is a stream in java?
What is ehcache in java?
What is difference between loosely coupled and tightly coupled in java?
What is the difference between collection and stream?
What is meant by annotations in java?
What is the use of entity class in java?
What is transaction management in java?
What is the means of java?
What is the resourcebundle class?