当前位置:数据库 > Access >>

JSP连接数据库:初学JDBC+access数据库

这是一个简单的jdbc odbc桥的连接

首先要建立数据库  打开控制面板 在配置系统dsn中设置数据源名字为TestData

import java.sql.*;
class SqlTest
{
 public static void main(String[] args) throws Exception
 {
  String driver="sun.jdbc.odbc.JdbcOdbcDriver";
  String url="jdbc:odbc:TestData";    //数据源名字为TestData
  Connection con=null;
  Statement stmt=null;
  ResultSet rs=null;
  try
  {
   Class.forName(driver);
   con=DriverManager.getConnection(url);
   stmt=con.createStatement();
   rs=stmt.executeQuery("select * from adm");//表名是adm
   while(rs.next())
   {
    String s1=rs.getString("ID");
    System.out.println(s1);
    String s2=rs.getString("admin");
    System.out.println(s2);
    String s3=rs.getString("password");
    System.out.println(s3);
   }
  }catch(Exception e)
  {
   e.printStackTrace();
  }
  rs.close();
  con.close();
 }
}

下面的是jsp实现(道理基本一样):

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <head>
               <title>JDBCTest Page</title>
    </head>
         <body>
            <h1>JDBCTest Page</h1>
            <table border="1" align="center">
                <thead>
                    <tr>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                    <tr>
                        <td>ID</td>
                        <td>用户:</td>
                        <td>密码:</td>
                        <!--td>E-MAIL:<td-->
                        <td>e-mail密码:</td>
                    </tr>
<%
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     String url="jdbc:odbc:TestData";
     String username="";
     String password="";
     Connection con=DriverManager.getConnection(url,username,password);
     Statement stmt=con.createStatement();
     ResultSet rs=stmt.executeQuery("select * from adm");
     while(rs.next())
     {
%>
                        <tr>
                        <td><%=rs.getString("ID")%></td>
                        <td><%=rs.getString("admin")%></td>
                        <td><%=rs.getString("password")%></td>
                    </tr>
                    <%
     }
     rs.close();
     con.close();

%>
         </table>        
    </body>
</html>
补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,