JSP connection to access database

Time:2021-10-22

1、 Establish database and ODBC data source

1. Establish jcc.mdb database and user table      
2. Add test data
3. Configure ODBC data source

2、 Under <% wwwroot% > /, create a new access database connection file select.jsp

The source code of select.jsp is as follows:

<%@page contentType=”text/html;charset=gb2312″%>
<%@page import=”java.sql.*”%>
<html>
<body>
<%
try{
  Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
}
catch(ClassNotFoundException e){
  out.print(e);
}
try{
  String url = “jdbc:odbc:jcc”;
  Connection conn = DriverManager.getConnection(url,”jcc”,”jsp.com.cn”);
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery(“Select * FROM user”);
  out.println(“User-list”+”<br>”);
  while(rs.next()){
    out.print(rs.getString(1)+” “);
    out.print(rs.getString(2)+”<br>”);
  }
  rs.close();
  stmt.close();
  conn.close();
}
catch(Exception ex){
  out.print(ex);
}
%>
</body>
</html>

4、 Run http://localhost/Select.jsp , the results are as follows:

User-list
1 Corebit
2 Ivan

It means that the database connection is successful! congratulations! congratulations!

Otherwise, please check the relevant settings of the data source. There is a high possibility of error!

Postscript:

It is often asked how to make JSP access access database without ODBC data source. In order to solve this mystery, close up the following connection code for reference! Jcc.mdb and select.jsp are located under <% wwwroot% > (root directory).

The source code of the rewritten select.jsp is as follows:

<%@page contentType=”text/html;charset=gb2312″%>
<%@page import=”java.sql.*”%>
<html>
<body>
<%
try{
  Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
}
catch(ClassNotFoundException e){
  out.print(e);
}
try{
  String strDirPath=application.getRealPath(request.getRequestURI());
  strDirPath=strDirPath.substring(0,strDirPath.lastIndexOf(‘\\’))+”\\”;
  String url = “jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=”+strDirPath+”jcc.mdb”;
  Connection conn = DriverManager.getConnection(url);
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery(“Select * FROM user”);
  out.println(“User-list”+”<br>”);
  while(rs.next()){
    out.print(rs.getString(1)+” “);
    out.print(rs.getString(2)+”<br>”);
  }
  rs.close();
  stmt.close();
  conn.close();
}
catch(Exception ex){
  out.print(ex);
}
%>
</body>
</html>

The running result should be the same as that when using ODBC!

* Note: the file name select.jsp is case sensitive!

I hope this article can be helpful for your JSP connection to access database!
==========================================
Only JDBC ODBC bridges can be used to connect    
   You want to set up an ODBC data source    
   Then connect    
  String     dbdriver   =   “oracle.jdbc.driver.OracleDriver”;   
                   String       dbname    =    “jdbc:oracle:thin:@192.168.0.101:1521:orcl”;// Modify according to your situation    
                   String       user    =    “system”;// user name    
                   String     password    =    “manager”;// password    
                  Connection   conn   =   null;   
                  Statement   stmt   =   null;   
                  ResultSet   rs   =null;   
   String     sql=”select    *    from     Table name “// Modify according to the actual situation    
  try   
                              {   
                                      Class.forName(dbdriver);   
                              }   
                              catch(java.lang.ClassNotFoundException   e){   
                                          System.err.println(“Class   access_dbconnect   not   fount!”+e.getMessage());   
                                        }   
  conn=DriverManager.getConnection(dbname,user,password);   
                                      Statement   stmt=conn.createStatement();   
                                      rs=stmt.executeQuery(sql); 
=========================================
sDBDriver   =   “sun.jdbc.odbc.JdbcOdbcDriver”;   
                   sConnStr    =    “JDBC: ODBC: ODBC name”;    
                  conn   =   null;   
                  rs   =   null;   
                  try   
                  {   
                          Class.forName(sDBDriver);   
                  }   
                          conn   =   DriverManager.getConnection(sConnStr);   
                          Statement   statement   =   conn.createStatement();   
                          rs   =   statement.executeQuery(s);   
   You can create an access connection in the ODBC data source, and then change the ODBC name in the above code to your ODBC data source connection name.

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]