JSP connection to access database (not through the method of establishing ODBC data source)

Time:2021-10-21

one   Create the folder “accessdb” under the site (my site is e: \ javatest), and the database “test. MDB” and table “stu” under the folder. The stu field is   id,stuname        The path of test.mdb is e: \ javatest \ accessdb   below
As shown below:

2. Establish the test connection database file “accesstest. JSP” under the site (E: \ javatest)
The code is as follows:

<%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”” %>
<!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=gb2312″>
< title > access connection test < / Title >
</head>

<body>
<%
try{
  Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
}
catch(ClassNotFoundException e){
  out.print(e);
}
try{
  // Absolute path
  //String url = “jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=E:/javatest/AccessDb/test.mdb”;
  // among   E:/javatest/AccessDb/test.mdb   Is the absolute path where the database is located

  // Relative path
   String   strDirPath=getServletContext().getRealPath(“/”);               // Get the absolute path of the site: e: \ javatest\
  //out.print(strDirPath+”<br>”);

   strDirPath=strDirPath.replace(‘\\’,’/’);                              // Replace ‘\’ with ‘/’        E:/javatest/
  //out.print(strDirPath+”<br>”);

  String url = “jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=”+strDirPath+”AccessDb/test.mdb”;
  //out.print(url+”<br>”);

  //String url = “jdbc:odbc:accesstest”;
  // Establish ODBC data source connection

  Connection conn = DriverManager.getConnection(url);
  Statement stmt = conn.createStatement();
   ResultSet   rs  =  stmt.executeQuery(“Select  *  FROM   stu”);               // Get the record in the stu table
  out.println(“Table-list”+”<br>”);
  while(rs.next()){
    out.print(rs.getInt(1)+” “);
    out.print(rs.getString(2)+”<br>”);
  }
  rs.close();
  stmt.close();
  conn.close();
}
catch(Exception ex){
  out.print(ex);
}
%>
</body>
</html>

The operation results are as follows:

Recommended Today

IOS segmented pop-up window

Recently, I encountered a demand similar to the segmented pop-up window of Gaode map (as shown in the figure below). I didn’t find relevant information on the Internet, so I wrote one myself, hoping to provide you with ideas. Realization effect The idea is that when the pop-up window is in the first segment, add […]