Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to link JSP to database file (Access 2007)

 
Jane Ommy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic