<%@page import="java.sql.*"%>
<%@page contentType="text/html;charset=gb2312"%>
<html>
<body>
<%
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs 为你的数据库的
String user = "sa";
String password = "sa";
Connection conn = DriverManager.getConnection(url, user,
password);
Statement stmt = conn.createStatement();
String sql = "select job_id,job_desc from jobs";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
%>
您的第一个字段内容为:
<%=rs.getString(1)%>
<br>
您的第二个字段内容为:
<%=rs.getString(2)%>
<br>
<%
}
%>
<%
out.print("数据库操作成功,恭喜你");
%>
<%
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
<%@page contentType="text/html;charset=gb2312"%>
<html>
<body>
<%
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs 为你的数据库的
String user = "sa";
String password = "sa";
Connection conn = DriverManager.getConnection(url, user,
password);
Statement stmt = conn.createStatement();
String sql = "select job_id,job_desc from jobs";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
%>
您的第一个字段内容为:
<%=rs.getString(1)%>
<br>
您的第二个字段内容为:
<%=rs.getString(2)%>
<br>
<%
}
%>
<%
out.print("数据库操作成功,恭喜你");
%>
<%
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
private final String DBDRIVER ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private final String DBURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
private final String DBUSER = "sa";
private final String DBPASSWORD = "weilan";
private Connection conn = null;
public DataBaseConnection(){
try {
Class.forName(DBDRIVER) ;
this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
}
catch (Exception e) {}
} public Connection getConnection(){
return this.conn ;
} public void close() {
try {
this.conn.close() ;
}
catch (Exception e) {}
}
}
http://localhost:8080/test.jsp
检查你数据库打了补丁没有,sql2000需要打sp3补丁,才能在java中访问