首先要建立odbc数据源
如
test
在jsp中
<%@page language="java" import="java.sql.*" contentType="text/html;
charset=gb2312" %>
<%//可以修改的参数
String ls_username = "username";
//数据库用户名
String ls_password = "pass";
//密码
String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
String databaseName="jdbc
dbc:chaxun";
Connection con = null;
try
{
//注册JDBC连接驱动
Class.forName(driverName);
//与数据库建立连接
con = DriverManager.getConnection(databaseName,ls_username,ls_password);
}
catch(Exception e)
{
out.println("Error");
out.println(e.toString());
}
%>
<html>
<head>
<title>测试数据库连接</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
int li_count = 0;
String ls_sql = "select * from JL_YUMI";
try
{
Statement Stmt = con.createStatement();
ResultSet rs = Stmt.executeQuery(ls_sql);
if(rs.next())
out.println(rs.getString(1));
out.println("<BR>");
out.println(rs.getString(2));
out.println("<br>");
out.println((rs.getString(3)));
out.println("<BR>");
if(rs != null)
rs.close();
if(Stmt != null)
Stmt.close();
}
catch(Exception e)
{
out.println("<br>2" + e.toString() + "<br>");
}
out.println("测试成功!");
%>
</body>
</html>
<%
try
{
if(con != null)
con.close();
}
catch(SQLException sqle)
{
out.println(sqle.toString());
}
%>