servlet-jdbc

servlet-jdbc

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

public class ServletInsertingDataUsingHtml extends HttpServlet{ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter pw = response.getWriter(); String connectionURL = "jdbc:odbc:shweta"; Connection connection; try{ String username = request.getParameter("username"); String password = request.getParameter("password"); pw.println(username); pw.println(password); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection(connectionURL); PreparedStatement pst = connection.prepareStatement("insert into emp_info values(?,?)"); pst.setString(1,username); pst.setString(2,password); int i = pst.executeUpdate(); if(i!=0){ pw.println("
Record has been inserted"); } else{ pw.println("failed to insert the data"); } } catch (Exception e){ pw.println(e); } } **//where my dsn name is shweta,database name is db1 and table name is emp_info. i am getting 404 error that is requested resource not available. is there a error in database connectivity or in my program logic?please let me know as soon as possible**

View Answers

January 5, 2011 at 1:35 PM

Hi Friend,

Follow these steps:

1)Go to the start->Control Panel->Administrative Tools-> data sources.

2)Click Add button and select the driver Microsoft Access Driver(*.mdb).

3)After selecting the driver, click finish button.

4)Then give Data Source Name and click ok button.

5)Your DSN will get created.

6) Compile your servlet and restart the server and run your servlet.

Here is your required servlet code:

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

public class ServletInsertingDataUsingHtml extends HttpServlet{ 
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html"); 
        PrintWriter pw = response.getWriter();
        String connectionURL = "jdbc:odbc:student"; 
        Connection connection; 
        try{ String username = request.getParameter("username"); 
        String password = request.getParameter("password");
        pw.println(username); 
        pw.println(password);
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connection = DriverManager.getConnection(connectionURL); 
        PreparedStatement pst = connection.prepareStatement("insert into login(username,password) values(?,?)");
        pst.setString(1,username); 
        pst.setString(2,password);
        int i = pst.executeUpdate();
        if(i!=0){
        pw.println("Record has been inserted"); 
        } 
        else{
            pw.println("Failed to insert the data");
            } 
            }
            catch (Exception e){ 
                pw.println(e);
                } 
                }
}

Here student is our dsn and login is our database table.

Thanks









Related Tutorials/Questions & Answers:
servlet - JDBC
= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle","scott... con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle... and password but "jdbc:oracle:thin:@localhost:1521:oracle" in this url line
servlet-jdbc
servlet-jdbc  import java.io.*; import java.lang.*; import java.sql...."); PrintWriter pw = response.getWriter(); String connectionURL = "jdbc:odbc:shweta.... 5)Your DSN will get created. 6) Compile your servlet and restart the server
Advertisements
regarding ms acces database and servlet - JDBC
a servlet i got a sqlexception saying datasource not found and it told some thing about... name (e.g emp) 6. Click "Ok" and then try the following Servlet code where...("sun.jdbc.odbc.JdbcOdbcDriver"); connection = DriverManager.getConnection("jdbc:odbc
update
update  how can i update multiple records in database using jsp ,servlet and jdbc based on selection of checkbox in jsp
Online quiz mini project
Online quiz mini project  Sir i need a online quiz program coding using java servlet, jdbc odbc connection with ms. access. If possible send that source code to me. Thank you
JAVA - JDBC
JAVA  WRITE A PROGRAM USING SERVLET AND JDBC FOR DEVELOPING AN ONLINE APPLICATION FOR THE SHOPPING OF COMPUTER SCIENCE BOOK.YOU HAVE TO CREATE... the following links to have sample Servlet and JDBC applications. http
J2EE - Java Server Faces Questions
On http://www.roseindia.net/ * JSP * Servlet * JDBC
problem in creating web application using servelet, jsp, jdbc and xml - JSP-Servlet
problem in creating web application using servelet, jsp, jdbc and xml  Using Servlet, JSP, JDBC and XML create a web application for a courrier... the technologies JSP,Servlet and JDBC etc... Thanks
Are J2EE and Java Advanced the same?
application development. It includes JSP, Servlet, JDBC, EJB etc.. Advanced Java
i can not connect to database in servlet - JSP-Servlet
the tutorial in this site on servlet and JDBC. I did all the proccedure for connecting...(""); out.println("Servlet JDBC"); out.println(""); out.println("Servlet JDBC... offer .   Hi friend, Code to connect to database in servlet
i can not connect to database in servlet - JSP-Servlet
the tutorial in this site on servlet and JDBC. I did all the proccedure for connecting..."); PrintWriter out = response.getWriter(); out.println(""); out.println("Servlet JDBC"); out.println(""); out.println("Servlet JDBC"); out.println
Java servlet - Java Beginners
Java servlet  Thank you for your kind reply. please send Servlet with JDBC connection code  Hi Friend, We are providing you a code that will connect the servlet to database and insert the data. import
execution of java program
of the servlet-jdbc program on tomcat 5.5 server.. PlS provide..the execution steps... Foundation\Tomcat 5.5\common\lib\servlet-api.jar";"C:\Program Files\Apache Software..."; is this correct way ?THANKYOU!!!   Put servlet-api.jar and mysql-connector.jar
JSP-Servlets-JDBC
be JSP-->Servlet-->JDBC-->Services-->JSP 3, Update - The table created... should be navigated to a Servlet page and then the data should be passed to the database table using JDBC and DAO. 2, View - The table created in the database
Inserting data on a database in servlet - JSP-Servlet
Inserting data on a database in servlet  Hi I am following the tutorial in this site on servlet and JDBC. I did all the proccedure for connecting to a databse by a servlet.I used the example in "Inserting Data In Database table
error got minus one from read call - JSP-Servlet
JDBC"); out.println(""); out.println("Servlet JDBC...error got minus one from read call  hiiii while connecting my servlet...) the code is as follows: // *DataBase Connectivity from the Servlet. import
Is spring boot easy to learn
Is spring boot easy to learn  Hi, I am a software developer with over 2 years experience in JSP, Servlet, JDBC and Struts. For my clients I have developed applications in these technologies. Now there is some requirement in my
servelet and jdbc programming - XML
servelet and jdbc programming  Write a program using Servlet and JDBC... a servlet class "Student.java" import java.io.*; import java.lang.*; import... pw = response.getWriter(); String connectionURL = "jdbc:mysql://localhost
What's New?
Java JSP Servlet, JDBC, Hibernate, Struts 1, Struts 2, JSF... Tutorial | Java Servlet Tutorial  | Java Swing Tutorial | JEE 5 Tutorial  | JDBC Tutorial  | J2ME Tutorial | JSP
Is spring boot easy to learn
Is spring boot easy to learn  Hi, I am a software developer with over 2 years experience in JSP, Servlet, JDBC and Struts. For my clients I have developed applications in these technologies. Now there is some requirement in my
styling jsp - JSP-Servlet
these links very useful. Core Java JSP Servlet JDBC Hibernate
Java Developer Training
and work there way up. The Java Developer training program includes the Servlet, JDBC, JSP, Hibernate, Struts, Java Framework, etc. These are the advance
Tomcat Quick Start Guide
study: JSP Servlet JDBC Struts Hibernate Spring... application using JSP, Servlets and JDBC technologies. In this quick and very... exploration! ADS_TO_REPLACE_1 Tomcat JSP  and servlet Tutorial sections
How to delete the row from the Database by using while loop in servlet
Servlet (JDBC). you entered 'ramanujan' is 9 character but in the database... server database by using Servlet program (Tomcat server). In Database table..."); Connection c=DriverManager.getConnection("jdbc:odbc:hari","raman
Struts 2 Tutorial and Examples
Servlet JDBC Spring Framework Even if you don't know Spring
JSP CRUD Application
Servlet, JSP, and the JDBC. We will understand here the basic purpose of creating... the table. We can also create a CRUD application using JSP, Servlet, And the JDBC. Here is the video tutorial of: "JSP CRUD application example"ADS

Ads