我写了一个newform.jsp.代码如下:<%@page contentType="text/html"%>
<%@ page pageEncoding="GB2312"%>
<html>
<head>
<title>建立与数据库连接 </title>
</head>
<body>
<form action="handsql.jsp" method="get">
请输入你要连接的数据库名:<input type="text" name="database" size="10" >
<input type="submit" value="提交" >
</form>
</body>
</html>又写了一个handsql.jsp页面代码如下:<%@page contentType="text/html"%>
<%@ page pageEncoding="GB2312"%>
<html>
<head>
<title>bean页面</title>
</head>
<body>
<jsp:useBean id="sql" scope="application" class="usebean.linksql"></jsp:useBean>
<jsp:setProperty name="sql" property="connection" param="database"/>
取得与数据库连接:<jsp:getProperty name="sql" property="connection"/>
该数据库的表form的内容是:</br>
<jsp:setProperty name="sql" property="resultset" value="select * from student"/>
<table >
<th>学号</th><th>姓名</th><th>分数</th>
<tr>
<td>
<%sql.getResultset().getString(1); %>
</td>
</tr><tr>
<td>
<%sql.getResultset().getString(2); %>
</td>
</tr><tr>
<td>
<%sql.getResultset().getString(3); %>
</td>
</tr></table>
</body>
</html>
其中 javabean页面代码如下:
package usebean;
import java.sql.*;
public class linksql { private Connection connection=null;
private ResultSet resultset=null;
public linksql()
{
}
public void setConnection(String u){
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String uri="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=u";
try {
connection=DriverManager.getConnection(uri, "123", "123");
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
return connection;
}
public void setResultset(String query){
try { Statement state=connection.createStatement();
resultset=state.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet getResultset(){
return resultset;
}
}运行后报错:
org.apache.jasper.JasperException: Can't find a method to write property 'connection' of type 'java.sql.Connection' in a bean of type 'usebean.linksql'
org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:368)
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:307)
org.apache.jsp.handsql_jsp._jspService(handsql_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.27 logs.
--------------------------------------------------------------------------------
什么问题呢?
<%@ page pageEncoding="GB2312"%>
<html>
<head>
<title>建立与数据库连接 </title>
</head>
<body>
<form action="handsql.jsp" method="get">
请输入你要连接的数据库名:<input type="text" name="database" size="10" >
<input type="submit" value="提交" >
</form>
</body>
</html>又写了一个handsql.jsp页面代码如下:<%@page contentType="text/html"%>
<%@ page pageEncoding="GB2312"%>
<html>
<head>
<title>bean页面</title>
</head>
<body>
<jsp:useBean id="sql" scope="application" class="usebean.linksql"></jsp:useBean>
<jsp:setProperty name="sql" property="connection" param="database"/>
取得与数据库连接:<jsp:getProperty name="sql" property="connection"/>
该数据库的表form的内容是:</br>
<jsp:setProperty name="sql" property="resultset" value="select * from student"/>
<table >
<th>学号</th><th>姓名</th><th>分数</th>
<tr>
<td>
<%sql.getResultset().getString(1); %>
</td>
</tr><tr>
<td>
<%sql.getResultset().getString(2); %>
</td>
</tr><tr>
<td>
<%sql.getResultset().getString(3); %>
</td>
</tr></table>
</body>
</html>
其中 javabean页面代码如下:
package usebean;
import java.sql.*;
public class linksql { private Connection connection=null;
private ResultSet resultset=null;
public linksql()
{
}
public void setConnection(String u){
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String uri="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=u";
try {
connection=DriverManager.getConnection(uri, "123", "123");
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
return connection;
}
public void setResultset(String query){
try { Statement state=connection.createStatement();
resultset=state.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ResultSet getResultset(){
return resultset;
}
}运行后报错:
org.apache.jasper.JasperException: Can't find a method to write property 'connection' of type 'java.sql.Connection' in a bean of type 'usebean.linksql'
org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:368)
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:307)
org.apache.jsp.handsql_jsp._jspService(handsql_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.27 logs.
--------------------------------------------------------------------------------
什么问题呢?
解决方案 »
- 读取Properties出错,求高手指正!
- 求救!关于flex用httpservice给servlet传值问题!
- spring2.0+hibernate3.2中,oracle中产生大量的inactive session,如何解决?
- LOG4J使用问题
- 用POI在excel中导入数据时,为什么只能导入3000多字节
- 如果一个表里有几百万条记录,要采用分页的话,不影响速度,你该如何做?该采用什么机制?
- Ibm6000,如何设置让他从光驱启动。我要装LINUX ,急!!
- 购物车,怎么来控制用户所选物品的种类?他能无限选吗?给点建议,谢谢
- 请问,我如何让 cookie 失效 或者 删除 cookie
- 关于Servlet编译的问题,麻烦那位帮我分析一下
- javascript里获得动态表格的行数
- 谁帮小弟弄下递归
不符合规范吧,应该是
public void setConnection( Connection u){
即<jsp:setProperty name="sql" property="connection" param="database"/>
传递的是数据库名。
如用public void setConnection( Connection u){ 无法传值啊。
即<jsp:setProperty name="sql" property="connection" param="database"/>
传递的是数据库名。
如用public void setConnection( Connection u){ 无法传值啊。
和
public void setConnection(String u)
之间的问题
package db;import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;public class Dbconn { private final static String clazz_name = "sun.jdbc.odbc.JdbcOdbcDriver"; private final static String url = "jdbc:odbc:web";
// private static String user_name = "sa";
// private static String password = "123"; static {
try {
Class.forName(clazz_name); } catch (Exception e) {
e.printStackTrace();
}
} public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, null, null);
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;/**
* ConnectionManager.
* <p>Title: </p> *
* <p>Description: </p> *
* <p>Copyright: Copyright (c) 2005</p> *
* <p>Company: 北京阿博泰克北大青鸟信息技术有限公司</p> *
* @author luohao
* @version 1.0
*/
public class ConnectionManager { /**
* DRIVER_CLASS
*/
private static final String DRIVER_CLASS =
"sun.jdbc.odbc.JdbcOdbcDriver";
/**
* DATASOURCE
*/
private static final String DATASOURCE = "jdbc:odbc:NewsDataSource";
// private static final String DRIVER_CLASS =
// "com.microsoft.jdbc.sqlserver.SQLServerDriver";
/**
* DATABASE_URL
*/
private static final String DATABASE_URL =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=News";
/**
* DATABASE_USRE
*/
private static final String DATABASE_USRE = "sa";
/**
* DATABASE_PASSWORD
*/
private static final String DATABASE_PASSWORD = "sa"; /**
* getConnction
* @return Connection
*/
public static Connection getConnction() {
Connection dbConnection = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// dbConnection = DriverManager.getConnection(DATABASE_URL, DATABASE_USRE,
// DATABASE_PASSWORD); dbConnection = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://teacher:1433;Databasename=News",
"sa", ""); } catch (Exception e) {
e.printStackTrace();
} return dbConnection;
} /**
* closeConnection
* @param dbConnection Connection
*/
public static void closeConnection(Connection dbConnection) {
try {
if (dbConnection != null && (!dbConnection.isClosed())) {
dbConnection.close();
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
} } /**
* closeResultSet
* @param res ResultSet
*/
public static void closeResultSet(ResultSet res) {
try {
if (res != null) {
res.close();
res = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* closeStatement
* @param pStatement PreparedStatement
*/ public static void closeStatement(PreparedStatement pStatement) {
try {
if (pStatement != null) {
pStatement.close();
pStatement = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}}
这是俺的一个连接数据库的类,不晓得你能用多少
运行后报错:
org.apache.jasper.JasperException: Can't find a method to write property 'connection' of type 'java.sql.Connection' in a bean of type 'usebean.linksql'
org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:368)
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:307)
org.apache.jsp.handsql_jsp._jspService(handsql_jsp.java:59)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.27 logs. 我想将他改正确,不知怎么改?