关于servlet中数据库的一个简单问题(可把我给难死了),望大家来看看

jncz 2003-08-28 01:18:05
刚开始搞servlet,结果出师不利,数据库搞不通。忘大侠们帮忙
以下是代码,出错的地方我已用//标出来了
package newspublic;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletContext;
import java.sql.*;
/**
* <p>Title: new</p>
* <p>Description: public news</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: xjtu</p>
* @author jncz
* @version 1.0
*/

public class newspublic_ac
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GB2312";
Connection con=null;
//Initialize global variables
public void init() throws ServletException {
try{
String driver="org.gjt.mm.mysql.Driver";
Class.forName(driver).newInstance();
}
catch(Exception e){System.out.print(e.getMessage());}

}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String url = "jdbc:mysql://localhost:3306/student_info?user=11&password=11";
Statement stmt = null;
ResultSet rs = null;
int i=0;
try {
Connection con = DriverManager.getConnection(url);
stmt = con.createStatement();
}
catch (Exception ex) {
out.print(ex.getMessage());
}
try {
String sql = "select * from info";
rs = stmt.executeQuery(sql);
}
catch (Exception e) {
out.print(e.getMessage());
}
try {
rs.next();
out.print(rs.isFirst());//这里显示为true
out.print(rs.getString("id"));//这里显示为Column 'id' not found
}catch(Exception exx){out.print(exx.getMessage());}
}

//Clean up resources
public void destroy() {
}
}
...全文
73 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
jncz 2003-08-28
  • 打赏
  • 举报
回复
问题解决了
就是字体问题

谢谢大家的支持。
jncz 2003-08-28
  • 打赏
  • 举报
回复
会不会是字体问题

BG2312和ISO-8859-1之间的一些问题呢
javatech 2003-08-28
  • 打赏
  • 举报
回复
呵呵,我觉得有可能是根本没取到数据库连接的原因,你加些调试信息查一下是否取到了数据库连接!
jncz 2003-08-28
  • 打赏
  • 举报
回复
其他表都不行,就是去掉rs.isFirst()也还是不行。
直接在jsp里面用bean连接数据库是正常的。
在servlet里面下面这句式可以运行的。(数据库mysql)
out.print(rs.getRow());
wangyanqiu 2003-08-28
  • 打赏
  • 举报
回复


帮你up
jncz 2003-08-28
  • 打赏
  • 举报
回复
其他字段也是一样的,都没办法显示出来总是报错
lmh7607 2003-08-28
  • 打赏
  • 举报
回复
有的驅動rs.isFirst()是不認的.
你的問題應該是個很低級的問題,仔仔細細檢查,或者用別的table去測試.
javatech 2003-08-28
  • 打赏
  • 举报
回复
呵呵,这个表有没有别的字段,取别的字段试一试!
jncz 2003-08-28
  • 打赏
  • 举报
回复
还是不行
大家的方法我都试过了
rocsoar 2003-08-28
  • 打赏
  • 举报
回复
Servelet 好象没问题啊。

你把
try {
rs.next();
out.print(rs.isFirst());//这里显示为true
out.print(rs.getString("id"));//这里显示为Column 'id' not found
}catch(Exception exx){out.print(exx.getMessage());}

换成

try {
while(rs.next()) {
//out.print(rs.isFirst());//这里显示为true
out.print(rs.getString("id"));//这里显示为Column 'id' not found
}
}catch(Exception exx){out.print(exx.getMessage());}
试试看,估计是Mysql数据库中,执行rs.isFirst()的时候,游标位置改变造成的吧。(瞎猜的,不懂Di说)
jncz 2003-08-28
  • 打赏
  • 举报
回复
up
jncz 2003-08-28
  • 打赏
  • 举报
回复
我刚测试了
con不为空,rs不为空,而且我加了这么一些语句进行测试
rs.next();

//out.print(rs.getString("id"));

if(rs.isLast()){out.print("last");}else{out.print("not last");}//显示为not last

if(rs.isFirst()){out.print("first");}else{out.print("not first");}//显示为first

if(rs.isAfterLast()){out.print("afterlast");}else{out.print("not after last");}//显示为not after last

if(rs.isBeforeFirst()){out.print("before first");}else{out.print("not before first");}//显示为not before first
这说明的确已经指向了数据表的第一项。可是为题是如果加上rs.getString("id");//id字段也存在,他就会显示Column 'id' not found.如果写成rs.getString(1);或者2等等,总之不大于总字段数目的数,他就会显示“p”,仅仅一个字母,而且还是我数据表里面根本没有的内容。这到底怎么回事,我写的整个servlet有问题吗,数据库连接有没有问题,大家帮我看看。
afeicat 2003-08-28
  • 打赏
  • 举报
回复
说明没有取出数据,可能数据库连接有问题,
Connection con = DriverManager.getConnection(url);可能返回为空
xinshou1979330 2003-08-28
  • 打赏
  • 举报
回复
if(!rs.islast() ){
out.print(rs.isFirst);
out.print(rs.getString("id"));
}

这样可以么?
jncz 2003-08-28
  • 打赏
  • 举报
回复
数据库中是有id字段的。
out.print(rs.next())显示为false

那怎么解决呢
jncz 2003-08-28
  • 打赏
  • 举报
回复
帖子被淹了
up
afeicat 2003-08-28
  • 打赏
  • 举报
回复
在数据库中有没有id字段?判断一下rs.next()是否为空?
if(rs.next() == null){
out.print(rs.isFirst());//这里显示为true
out.print(rs.getString("id"));//这里显示为Column 'id' not found
}

81,115

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧