wood burning stoves 2.0*
The moose likes JSP and the fly likes How to link JSP to database file (Access 2007) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to link JSP to database file (Access 2007)" Watch "How to link JSP to database file (Access 2007)" New topic
Author

How to link JSP to database file (Access 2007)

Jane Ommy
Greenhorn

Joined: Apr 16, 2012
Posts: 1
Check my code below. I have tried to compile but is not working. Any help please.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!//JSP Declaratives begins with '<%!' and ends with '&>'%>
<% //Part A: retrieve form values and perform validation---------
String fName = "";
String lName = "";
String pword = "";

//Retrieve form value when the form is submitted
fName = request.getParameter("firstName");
lName = request.getParameter("lastName");
pword = request.getParameter("password");

//Part B: Display informaton to user
out.println("Dear " +
fName.toUpperCase() + " " + lName.toUpperCase() + "<BR>" +
"Thank you for registration.<BR>");

//End Part B--------------------------------------------------
%>
<table border=1>
<tr><th>First Name</th><th>Surname</th></tr>

<%
out.println("Your details have been recorded. <BR><BR>");

//Part C: Connect to a database (i.e. Microsoft Access)
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


//type the exact path name: "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=
//followed up by the absolute file address
String accdb = "jdbc:odbc:Driver={Microsoft Access Driver (*.accdb)};DBQ=h:\\203KM\\MyWebsiteProject\\web\\DatabaseDemo.accdb";
Connection conn = DriverManager.getConnection(accdb);


//Part D: Insert form data into the database table, called UserDemo
Statement stm=conn.createStatement();

String sql = "INSERT INTO UserDemo VALUES ('" + fName + "','" + lName + "','" + pword + "')";
st.executeUpdate(sql);

//Part E: Retrieve data that matches teh crieteri from teh database table
ResultSet rs=st.executeQuery("select * from UserDemo " +
"WHERE FName = '" + fName + "' and LName ='" + lName + "'");
while (rs.next()){ %>
<tr><td><%=rs.getString("FName")%>
</td><td><%=rs.getString("SName")%>
</td></tr><%}rs.close();

st.close();
con.close();}

catch(Exception e){}
%>

</table>
<BR><BR>
Change your password? Click <a href="UpdateForm.html">here</a>.

</body>
</html>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to link JSP to database file (Access 2007)