• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem inserting data into MS Access

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am using jsp to insert data into MS Access using JSP. Functionality is such that there is a simple HTML form having a submit button
which is calling a JSP.The JSP connect to MS Access file and should update the values.The same code when i used with JAVA it is giving me the desired result but not with JSP.Please help..

HTML CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form METHOD="POST" action="CreateConnection.jsp" >
<input type="button" value="submit" /> //i have used input type submit also
</body>
</html>

JSP CODE
//CreateConnection.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page import="java.util.*" %>
<%@ page import="javax.sql.*;" %>

<%
java.sql.Connection con = null;
java.sql.Statement sta = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
out.println("Connection ok.");
sta =con.createStatement();
int c = sta.executeUpdate("INSERT INTO Customers"
+ " (ID, FirstName, SurName)"
+ " VALUES (2, 'raju', 'rama')");

sta.close();
con.close();
}catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}


%>
<h1>hellooooo</h1>
</body>
</html>
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harendra Sahani wrote:I am using jsp to insert data into MS Access using JSP.


Bad idea. JSP is a really poor technology choice for this.

The same code when i used with JAVA it is giving me the desired result but not with JSP


Then why not leave the code in a Java class where it belongs? Why move it to an inappropriate JSP?

 
Harendra Sahani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear Bibeault for the suggestion,but I have one html page where i have to implement this functionality.Is there any way to call Java class from a HTML page?I am restricted to use that simple HTML page to implement this funtionality.Thats leave me no other choice than to use JSP(As far as my knowledge concerned).Please suggest any otherway to implement this.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harendra Sahani wrote:Thats leave me no other choice than to use JSP...


That makes no sense. If you are restricted to HTML, how can you use JSP?

If you mean that the original form is an HTML page, then it can submit to a servlet just as easily as it can to a JSP. The format of the form page has no bearing whatsoever on your choice of technology.
 
Harendra Sahani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have update my code as per suggested by Bear Bibeault but even though after using servlet which in turn calling a POJO,it doesnot get me the desired result.I have tried several times but wihtout success.Please suggest how to modify the given below code:

[HTML]
<html>
<body style="background-color:gray">
<h1 align ="center">Update</h1>
<form method ="POST" action="check.do" >
Just go to servlet





<input type="SUBMIT">
</form>
</body>
</html>

SERVLET CODE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author Harendra.Sahani
*/
public class Trial extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out =response.getWriter();
out.println(" start updation
");
OdbcConnection od = new OdbcConnection();
od.updataDB();
out.println("check if updated
");
}
}

JAVA CODE:
import java.sql.*;
public class OdbcConnection {


public String updataDB(){
Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;

// Connect with a url string
con = DriverManager.getConnection("jdbc:odbc:Sample");
System.out.println("Connection ok.");
Statement sta =con.createStatement();
int c = sta.executeUpdate("INSERT INTO Customers"
+ " (ID, FirstName, SurName)"
+ " VALUES (1, 'Nitin', 'kanujia')");

sta.close();
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}
return("You have checked my code");
}

}

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the JDBC forum with an appropriate subject title change.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what happens if you run the code? TellTheDetails
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags.
 
Harendra Sahani
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i run the code it shows the messages i.e
[Message on HTML page]
Update
just Go to servlet

[after clicking submit button]
Start Updation
check if update

But it does not insert the data into the MS Access DB file as it happend with the pure java code.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it doesn't print the "Connection ok." message? Sounds like the connection can't be obtained. Did you define an ODBC data source called "Sample" on the server? Is it a SYSTEM data source (as opposed to a USER data source)?

As an aside, the JDBC/ODBC bridge is not suitable for a multi-threaded environment like a web app - it is not thread-safe (and also buggy). You're better off using a proper DB and driver.
 
reply
    Bookmark Topic Watch Topic
  • New Topic