哪位有jsp通过jdbc连接sql server 2000的例子,能给我吗?

kind123 2001-05-30 09:34:00
...全文
48 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kind123 2001-05-30
另外是不是Class.forName("a jdbc driver").newInstance();这句话中a jdbc driver要改我用的是sql server 2000 的驱动是weblogic.zip,如果改,如何改?
回复
kind123 2001-05-30
从上面的例子看不到sql server 2000 的数据库是什么?
回复
BrentIvan 2001-05-30
jsp+beans实现读取数据库的例子
Select.jsp

<html>
<head>
<title>Select everything from a database</title>
</head>
<body>

<jsp:useBean id="select" class="DataBaseSelect" scope="request">
</bean>

<% out.print(select.connect()); %>
<br>
<% out.print(select.select()); %>

<p>Format results

<br>
<%@ page import="java.util.Vector" %>
<% Vector aResult = select.getResult(); %>

<table>
<% for (int i=0; i < aResult.size(); i++) { %>
<tr>
<td>
<% out.print(aResult.elementAt(i)); %>
</td>
</tr>
<% } %>
</table>

</body>
</html>

DataBaseSelect.java
import java.sql.*;
import java.util.Vector;

/**
* DataBaseSelect.java
* Written by Morgan Catlin
* August 19, 1999
*
* Variables:
* Vector result = where I put the results of a select query
*
* Methods:
* Vector getResult() = returns result
* void setResult() = sets result
* String connect() = connects to a database
* String select() = selects information from a database
*/

public class DataBaseSelect {

private Vector result;

public DataBaseSelect() {
result = new Vector();
} // constructor DataBaseSelect

public String connect() {
try {
Class.forName("a jdbc driver").newInstance();
return "Driver Loaded!";
} catch (Exception E) {
return "Unable to load driver.";
}
}

public String select() {
try {
Connection C = DriverManager.getConnection("database connection string");

Statement Stmt = C.createStatement();

ResultSet myResult = Stmt.executeQuery("SELECT acolumn from atable");

while (myResult.next()) {
result.addElement(myResult.getString(1));
}

// Clean up
myResult.close();
Stmt.close();
C.close();
return "Connection Success!";
} catch (SQLException E) {
return "SQLException: " + E.getMessage();
}
}

/**
* Accessor for result
**/
public Vector getResult() {
return result;
}

/**
* Mutator for result
**/
public void setResult(Vector avector) {
result = avector;
}

} // class DataBaseSelect


回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2001-05-30 09:34
社区公告
暂无公告