這是一個簡單的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>