JSP连接数据库:初学JDBC+access数据库
程序员文章站
2022-06-22 13:47:12
这是一个简单的jdbc odbc桥的连接首先要建立 打开控制面板 在配置dsn中设置数据源名字为testdataimport java.sql.*;class sqltest{ ...
这是一个简单的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();
}
}
下面的是实现(道理基本一样):
<%@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>
首先要建立 打开控制面板 在配置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();
}
}
下面的是实现(道理基本一样):
<%@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>