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 is @resource annotation in java?
how can we create the ui using netbeans,having the code?
What is static class in java?
What is the use of entity class in java?
Who is at risk in java? : java security
What is a container in java?
Can we write lambda without functional interface?
What are the new features about Java se 8 ?
What is exe file in java?
What is microservices java?
What is java lang exceptionininitializererror?
How do you check if java is installed on windows command prompt?
What is the difference between swing and awt components?
What is optional in java 8? What is the use of optional?advantages of java 8 optional?
What is lambda value?