| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1044 人关注过本帖
标题:jsp链接mysql出错,帮看一下这两个文件.
只看楼主 加入收藏
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
 问题点数:0 回复次数:5 
jsp链接mysql出错,帮看一下这两个文件.
conn.java文件如下:

package conn;
import java.sql.*;
public class conn {
        String dbUrl = "jdbc:mysql://localhost:3306/user";
    String dbUser = "root";
    String dbPassword = "12345";

    Connection connect = null;
    ResultSet rs = null;

    public conn() {
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
        }
        catch(java.lang.ClassNotFoundException e) {
            System.err.println( e.getMessage());
        }
    }

    public ResultSet executeQuery(String sql) {
        try {
            connect = DriverManager.getConnection(dbUrl,dbUser,dbPassword);
            Statement stm = connect.createStatement();
            rs = stm.executeQuery(sql);
        }
        catch(SQLException ex) {
            System.err.println(ex.getMessage());
        }
        return rs;
    }
}

conn.jsp文件如下:

<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="connBean" scope="page" class="conn.conn"/>
<html>
<head>
    <title>使用Bean连接MYSQL数据库</title>
</head>
<body  bgcolor="#FFCCCC">
    <div align="center">
      <table width="60%" border="1">
        <tr>
            <td width="50%" bgcolor="#CCCCFF">
                  <div align="center"><font color="#FF0033"><b>学号</b></font></div>
              </td>
              <td width="25%" bgcolor="#CCCCFF">
                  <div align="center"><font color="#FF0033"><b>姓名</b></font></div>
              </td>
        </tr>
<%
    ResultSet rs = connBean.executeQuery("select * from info");
    while (rs.next()){
%>
           <tr>
            <td width="50%">
                  <div align="center"><%=rs.getString(1)%></div>
            </td>
              <td width="25%">
                <div align="center"><%=rs.getString(2)%></div>
              </td>
        </tr>
<%
    }
    rs.close();
%>
  </table>
</body>
</html>


提示 while (rs.next()){ 这一行出错.
请问有没有好的连接方式.
搜索更多相关主题的帖子: mysql jsp java 文件 
2008-02-28 23:02
kingyor
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2006-6-4
收藏
得分:0 
import
import java.sql.*;

public class PubSQL{

    private Connection conn;
    public ResultSet rs;
    private String sDBDriver;
    private String sConnStr;

    public PubSQL()
    {
        conn = null;
        sDBDriver = "org.gjt.mm.mysql.Driver";
        sConnStr = "jdbc:mysql://127.0.0.1:3306/"+dbname+"?user=root&password=123";
        try
        {
            Class.forName(sDBDriver);
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            System.out.println("Jdbc_conn():" + classnotfoundexception.getMessage());
        }
    } public ResultSet query(String s)
        throws Exception
    {
        try
        {
            s = new String(s.getBytes("UTF-8"), "ISO8859_1");
            conn = DriverManager.getConnection(sConnStr);
            Statement statement = conn.createStatement();
            rs = statement.executeQuery(s);
        }
        catch(SQLException sqlexception)
        {
            System.out.print("sql.executeQuery:" + sqlexception.getMessage());
        }
        return rs;
    }

    public void update(String sy)
        throws Exception
    {
        try
        {
            sy = new String(sy.getBytes("UTF-8"), "ISO8859_1");
            conn = DriverManager.getConnection(sConnStr);
            Statement statement = conn.createStatement();
            statement.executeUpdate(sy);
        }
        catch(SQLException sqlexception)
        {
            System.out.print("sql.executeQuery:" + sqlexception.getMessage());
        }
    }
}

[[it] 本帖最后由 kingyor 于 2008-2-29 09:58 编辑 [/it]]

宝哥啊宝哥~~命苦啊命苦啊~~~
2008-02-29 09:44
nhy520
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2006-5-9
收藏
得分:0 
应该把这个写成一个方法返回结果集,这样再调用就方便了

2008-02-29 14:48
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
能不能看一下我的那个错在哪里
或者大家有成功的能否提供一下代码?

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-02-29 17:02
论坛元老
Rank: 1
等 级:新手上路
帖 子:812
专家分:0
注 册:2008-3-31
收藏
得分:0 
import
import java.sql.*;

public class PubSQL{

&nbsp; &nbsp; private Connection conn;
&nbsp; &nbsp; public ResultSet rs;
&nbsp; ...
2008-04-02 17:04
Starlove
Rank: 1
等 级:新手上路
帖 子:169
专家分:0
注 册:2007-4-26
收藏
得分:0 
Bean里面throws exception
jsp里面try
信息太少
再说的详细一点
2008-04-03 10:39
快速回复:jsp链接mysql出错,帮看一下这两个文件.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032418 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved