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
In java thread programming, which method is a must implementation for all threads?
Why is lambda expression used?
I am trying to create a new universal user group. Why can't i? : java security
What is cookie in java?
What is orm in java?
What is ui framework in java?
What is @override annotation in java?
What is predicate in lambda expression?
When is static variable loaded? Is it at compile time or runtime? When exactly a static block is loaded in java?
What is cache in java?
What are microservices in java?
What is custom tag in java?
What are the advantages of java 8’s date and time api over old date api and joda time api?
differences between iterator and spliterator in java se 8?
In a barber shop there are 2 doors. customer come in 1 door, leave in other. minimum # of chairs. barber spend his life in cutting. always barber can cut 1 customer. few chairs in the shop. if barber busy customer waits, if chairs full, customer leave. if no customer, barber sleeps. treat barber and customer as 2 threads. you can use Semaphore class with arrive and depart and count as parameter.