JAVA代码:
package mybean;import java.io.*;
import java.sql.*;public class connbean {
private String pdm="";
private String username="root";
private String password="x7u7w2e1i4";
private String dbname="study";
private String tableName="product";
public void setPdm(String pdm){
this.pdm=pdm;
}
public String getPdm(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e)
{System.out.println("加载驱动器程序异常");} try{
String url="jdbc:mysql://localhost/"+dbname+"?useUnicode=true&characterEncoding=gb2312&user="+username+"&password="+password;
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement(); String sql="SELECT * FROM"+tableName;
ResultSet rs = statement.executeQuery(sql);
rs.last();
String pdm=rs.getString("pdm"); rs.close();
statement.close();
connection.close();
}catch(SQLException e)
{System.out.println("连接出现异常");}
return pdm;
}
}
JSP文件代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page contentType="text/html; charset=gb2312" pageEncoding="gb2312" %>
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=GB2312" >
<title>jsp connbean test</title>
</head>
<body>
<jsp:useBean id="bean1" class="mybean.connbean" scope="page"/>
<jsp:getProperty name="bean1" property="pdm"/>
</body>
</html>
运行时显示器没有任何显示,一片空白,请问是怎么回事?JAVA文件编译正常.
package mybean;import java.io.*;
import java.sql.*;public class connbean {
private String pdm="";
private String username="root";
private String password="x7u7w2e1i4";
private String dbname="study";
private String tableName="product";
public void setPdm(String pdm){
this.pdm=pdm;
}
public String getPdm(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e)
{System.out.println("加载驱动器程序异常");} try{
String url="jdbc:mysql://localhost/"+dbname+"?useUnicode=true&characterEncoding=gb2312&user="+username+"&password="+password;
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement(); String sql="SELECT * FROM"+tableName;
ResultSet rs = statement.executeQuery(sql);
rs.last();
String pdm=rs.getString("pdm"); rs.close();
statement.close();
connection.close();
}catch(SQLException e)
{System.out.println("连接出现异常");}
return pdm;
}
}
JSP文件代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page contentType="text/html; charset=gb2312" pageEncoding="gb2312" %>
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=GB2312" >
<title>jsp connbean test</title>
</head>
<body>
<jsp:useBean id="bean1" class="mybean.connbean" scope="page"/>
<jsp:getProperty name="bean1" property="pdm"/>
</body>
</html>
运行时显示器没有任何显示,一片空白,请问是怎么回事?JAVA文件编译正常.
ResultSet rs = statement.executeQuery(sql);
rs.last();
直接rs.last();那怎么能取到数据?你在这里打印下,看能不能取到数据。
这里好像是用rs.next(){}来接数据的。
String sql="SELECT * FROM "+tableName;
更本就不知道取的是哪一条吧,
你用rs.next()在循环体里取,并打印出来。你在试试
还有你确定把值放到了javaBean里了吗?
JAVA代码:
package mybean;import java.io.*;
import java.sql.*;public class connbean {
private String pdm="conn to mysql";
private String username="root";
private String password="*****";
private String dbname="study";
private String tableName="product";
public String getPdm(){
System.out.println(pdm);
String url="jdbc:MySQL://localhost/"+dbname+"?useUnicode=true&characterEncoding=gb2312";
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e) {
System.out.println("加载驱动器程序异常");
}
try{
Connection connection=DriverManager.getConnection(url,username,password);
Statement statement = connection.createStatement(); String sql="SELECT pdm FROM product WHERE id=1";
ResultSet rs = statement.executeQuery(sql);
System.out.println("数据库连接成功!");
String pdm=rs.getString("pdm"); rs.close();
statement.close();
connection.close();
}catch(SQLException e)
{System.out.println("连接出现异常");}
return pdm;
}
}
JSP文件不变.
我把联结字符串改成了"select pdm from product where id=1";
怎么还是不能正确的显示呢.好象我的
public String getPdm(){
以下的所有JAVA代码根本没有执行?
不是说这里很多高人的吗?
取值不是这样取的。楼主,你的代码看着好别扭。这是以前写的一个。希望对你有帮助。
private Connection ct = null;
private PreparedStatement ps = null;
private ResultSet rs =null;
public List getResultByPage(int pageNow,int pageSize){
List list = new ArrayList();
try{
int rowCount = 0;
int pageCount = 0;
ct = new ConnDB().getConn();
ps = ct.prepareStatement("select count(*) from users");
rs = ps.executeQuery();
if(rs.next()){
rowCount = rs.getInt(1);
}
pageCount = (rowCount+pageSize-1)/pageSize;
ps = ct.prepareStatement("select top "+pageSize+" * from users where " +
"userid not in (select top "+(pageSize*(pageNow-1)) +" userid from users)");
while(rs.next()){
UserBean ub = new UserBean();
ub.setUserId(rs.getInt(1));
ub.setUserName(rs.getString(2));
ub.setPassword(rs.getString(3));
ub.setEmail(rs.getString(4));
ub.setGrade(rs.getInt(5));
list.add(ub);
}
}catch (Exception e){
e.printStackTrace();
}finally{
try {
if(rs != null){rs.close();rs = null;}
if(ps != null){ps.close();ps = null;}
if(ct != null){ct.close();ct = null;}
} catch (Exception e) {
e.printStackTrace();
} }
return list;
}