SP Operations Access database
Recently in the use of JSP to write the company's Web site sample program, used in the JSP operation Access database, a lot of things are not difficult, but we do not know. ^-^ hehe, so now I wrote a small program, to the use of JSP operation Access database program demo as follows, I hope that we can save the pain of searching things.
First , I wrote a databasedemo.jsp page, the specific code is as follows, this is the specific process of operating the database.
databasedemo.jsp The page code is as follows:
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<%@ page import= "java.io.*"%>
<%
String usernamev= "";
String passwordv= "";
Try
{
$False $
Path
String p=request.getservletpath ();
string path= Request.getrealpath (P);
path=path.substring (0,path.lastindexof (""));
string dbpath=path+ "Userdatabase.mdb"; < Span class= "Apple-converted-space" >
String dbname= "";
String user= "";
//Connecting to the connection string for an Access database
String url= "Jdbc:odbc:driver={microsoft Access Driver (*.mdb)};D bq=" +dbpath;
// Create connection objects and statement objects.
class.forname ("Sun.jdbc.odbc.JdbcOdbcDriver");
connection conn=drivermanager.getconnection (URL);
statement stmt=conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable);
&NBSp
string sql= "select * from UserInformation";
resultset rs=stmt.executequery (SQL);
while (Rs.next ())
{
usernamev=rs.getstring ("UserName");
passwordv=rs.getstring ("PassWord
Br>
Out.print ("User name:" +rs.getstring ("UserName"));
Out.print ("User name:" +usernamev+ "<br>");
Out.print ("Password:" +PASSWORDV);
}
Rs.close ();
Stmt.close ();
Conn.close ();
}
catch (Exception e)
{//error
Out.print ("An error has occurred!") ");
}
%>
second , I set up a virtual directory for this page under the Tomcat server, while the test database Userdatabase.mdb and pages are placed under the same directory. Then enter the http://localhost:8080/myapp/sub/DatabaseDemo.jsp in IE to see the result.