代码如下:
dbconn.java如下:
---------------------------------------
package Border;
import java.sql.*; //完成环境设置,导入java.sql包
public class dbconn {
public dbconn()
{
}
//declare variable
private Connection conn = null;
private ResultSet rs = null;
private String server = "127.0.0.1";
private String port = "1433";
private String db = "publish";
private String user = "jieking";
private String pass = "0085529j225"; //change to your password
private String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=testDate"; public Connection getConn(){ //get database connection
try{
Class.forName(drivername).newInstance(); //载入驱动器
conn = DriverManager.getConnection(URL,user,pass); //连接到数据库
}
catch(Exception e){
e.printStackTrace();
}
return conn ;
} public ResultSet executeSQL(String str) {
try{
Statement stmt = conn.createStatement(); //语句接口
rs = stmt.executeQuery(str); //获得结果集
}
catch(Exception e){
e.printStackTrace();
}
return rs;
}
}
---------------------------------------
jsp引用代码如下:
----------------------------------------
<%@ page contentType="text/html;charset=GBK" import="java.sql.*;Border.dbconn"%> <jsp:useBean id="Border" scope="page" class="Border.dbconn" />
<%
ResultSet rs = null;
Connection conn = null;
conn = test.getConn() ;
rs = test.executeSQL("select * from book");
%>
<html>
<body>
<br>
<h2 align="center" > My first Jsp JavaBean Mysql </h2>
<br>
<table border="1" align="center">
<tr>
<th>
id
</th>
<th>
title
</th>
<th>
price
</th>
</tr> <%
while(rs.next()) {
%>
<tr>
<th>
<%=rs.getString("id")%>
</th>
<th>
<%=rs.getString("title")%>
</th>
<th>
<%=rs.getString("price")%>
</th>
</tr>
<%}%>
<%
rs.close();
conn.close();
%>
</table>
</body>
</html>
------------------------------------------------
dbconn.java如下:
---------------------------------------
package Border;
import java.sql.*; //完成环境设置,导入java.sql包
public class dbconn {
public dbconn()
{
}
//declare variable
private Connection conn = null;
private ResultSet rs = null;
private String server = "127.0.0.1";
private String port = "1433";
private String db = "publish";
private String user = "jieking";
private String pass = "0085529j225"; //change to your password
private String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=testDate"; public Connection getConn(){ //get database connection
try{
Class.forName(drivername).newInstance(); //载入驱动器
conn = DriverManager.getConnection(URL,user,pass); //连接到数据库
}
catch(Exception e){
e.printStackTrace();
}
return conn ;
} public ResultSet executeSQL(String str) {
try{
Statement stmt = conn.createStatement(); //语句接口
rs = stmt.executeQuery(str); //获得结果集
}
catch(Exception e){
e.printStackTrace();
}
return rs;
}
}
---------------------------------------
jsp引用代码如下:
----------------------------------------
<%@ page contentType="text/html;charset=GBK" import="java.sql.*;Border.dbconn"%> <jsp:useBean id="Border" scope="page" class="Border.dbconn" />
<%
ResultSet rs = null;
Connection conn = null;
conn = test.getConn() ;
rs = test.executeSQL("select * from book");
%>
<html>
<body>
<br>
<h2 align="center" > My first Jsp JavaBean Mysql </h2>
<br>
<table border="1" align="center">
<tr>
<th>
id
</th>
<th>
title
</th>
<th>
price
</th>
</tr> <%
while(rs.next()) {
%>
<tr>
<th>
<%=rs.getString("id")%>
</th>
<th>
<%=rs.getString("title")%>
</th>
<th>
<%=rs.getString("price")%>
</th>
</tr>
<%}%>
<%
rs.close();
conn.close();
%>
</table>
</body>
</html>
------------------------------------------------
解决方案 »
- commons dbutils 如何处理事务
- 以前使用session传递数据,现在为啥用actionForm了?
- 求java简单教务管理系统
- struts2判断用户密码不为空的时候出错,请看异常信息,和代码
- jsp网站怎么整合支付宝功能啊
- 请问JAKARTA的mailer标签为什么无法发送EMAIL
- 有那位大侠,有java excel api 的jxl.jar,有的话,发一份到我的邮箱[email protected]
- JSP注册登录的验证设计
- 各位大虾,关于cookie,在我的tomcat下出错,而在其他的tomcat就运行正常。
- !!!!!急!!一位在校的计算机专业的mm想请问各位大侠:如何用jsp对文本文件进行修改
- 奇怪! jsp传数值就行,传字符串怎么就不行呢? 请高手指教,谢谢,高分相赠!
- 滚动条
<%
ResultSet rs = null;
Connection conn = null;
conn = test.getConn() ;
rs = test.executeSQL("select * from book");
%>
改为:
<jsp:useBean id="Border" scope="page" class="Border.dbconn" />
<%
ResultSet rs = null;
Connection conn = null;
conn = Border.getConn() ;
rs = Border.executeSQL("select * from book");
%>
应该改成Border吧
ResultSet rs = null;
Connection conn = null;
dbconn test = new dbcon();
conn = test.getConn() ;
....
xiachedan(瞎扯蛋)不好意思,那里写错了。
public Connection getConn(){ //get database connection
Connection conn = null;
try{
Class.forName(drivername).newInstance(); //载入驱动器
conn = DriverManager.getConnection(URL,user,pass); //连接到数据库
}
catch(Exception e){
e.printStackTrace();
}
return conn ;
}
然后把类中定义的conn去掉
public ResultSet executeSQL(String str) {
try{
Statement stmt = conn.createStatement(); //语句接口
rs = stmt.executeQuery(str); //获得结果集
}
catch(Exception e){
e.printStackTrace();
}
return rs;
}
不那样的话那conn.createStatement(); 里的conn会变为没定义的标示符号。
The value for the useBean class attribute Border.dbconn is invalid
两个没关系,是你的Bean有问题!楼主试试直接生成定义对象,不用
<jsp:useBean id="Border" scope="page" class="Border.dbconn" /> 直接 Border.dbconn dbconn = new Border.dbconn()
不用<jsp:useBean id="Border" scope="page" class="Border.dbconn" />这个行吗
jsp文件加入Border.dbconn dbconn = new Border.dbconn()这句后仍然错误哪位能帮我整好这两段代码
dbconn.java
--------------------
package Border;import java.sql.*; //完成环境设置,导入java.sql包 public class dbconn {
public dbconn() {
try{
Class.forName(drivername); //载入驱动器
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
//declare variable
Connection conn = null;
ResultSet rs = null;
String user = "jieking";
String pass = "830924k19"; //change to your password
String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=publish"; public Connection getConn(){ //get database connection
try{
conn = DriverManager.getConnection(URL,user,pass); //连接到数据库
} catch(Exception e){
e.printStackTrace();
}
return conn ;
} public ResultSet executeQuery(String sql) {
try{
Statement stmt = conn.createStatement(); //语句接口
rs = stmt.executeQuery(sql); //获得结果集
} catch(Exception e){
e.printStackTrace();
}
return rs;
} public int executeUpdate(String sql){
int result=0;
try{
Statement stmt = conn.createStatement(); //语句接口
result = stmt.executeUpdate(sql); //获得结果集
}catch(Exception e){
e.printStackTrace();
}
return result;
}
}
--------------------------
usedbconn.jsp:
------------------
<%@ page contentType="text/html;charset=GBK" import="java.sql.*"%>
<%@ page import="Border.dbconn"%>
<jsp:useBean id="Border" scope="page" class="Border.dbconn" />
<%
ResultSet rs = null;
Connection conn = null;
conn = Border.getConn() ;
rs = Border.executeQuery("select * from book");
%>
<html>
<body>
<br/>
<h2 align="center" > My first Jsp JavaBean Mssql </h2>
<br/>
<table border="1" align="center">
<tr>
<th>
id
</th>
<th>
title
</th>
<th>
price
</th>
</tr> <%
while(rs.next()) {
%>
<tr>
<th>
<%=rs.getString("id")%>
</th>
<th>
<%=rs.getString("title")%>
</th>
<th>
<%=rs.getString("price")%>
</th>
</tr>
<%}%>
<%
rs.close();
conn.close();
%>
</table>
</body>
</html>
------------------------------------
虽然各位的话没起太大作用,但还是感谢大家