Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes problem inserting data into MS Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "problem inserting data into MS Access" Watch "problem inserting data into MS Access" New topic
Author

problem inserting data into MS Access

Harendra Sahani
Greenhorn

Joined: May 11, 2009
Posts: 9
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>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Harendra Sahani
Greenhorn

Joined: May 11, 2009
Posts: 9
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: May 11, 2009
Posts: 9
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("jdbcdbc: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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Moved to the JDBC forum with an appropriate subject title change.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
So what happens if you run the code? TellTheDetails
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags.
Harendra Sahani
Greenhorn

Joined: May 11, 2009
Posts: 9
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
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem inserting data into MS Access