当前位置: 代码迷 >> Java Web开发 >> 请问jsp连接mysql的有关问题
  详细解决方案

请问jsp连接mysql的有关问题

热度:187   发布时间:2016-04-17 10:44:22.0
请教jsp连接mysql的问题.
mysqlrs.java
Java code
package anhua;import java.sql.*;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class mysqlrs {    Connection con = null;     Statement stat = null;     PreparedStatement pstat = null;    ResultSet rs = null;    //无参数的构造函数     public mysqlrs(){}    //取得数据库连接     public Connection getCon()    {     try{       Class.forName("com.mysql.jdbc.Driver").newInstance();       String url = "jdbc:mysql://localhost:3306/anhua?user=root&password=123456&useUnicode=true&characterEncoding=utf-8";       con = DriverManager.getConnection(url);     }    catch(Exception ex)    {           //ex.printStackTrace();         //System.err.print("Exception");        //out.print( "yes ");    }     return con;       }    //执行数据库查询并返回查询结果    public ResultSet query(String sql)    {     try{   con = getCon();       stat = con.createStatement();       rs = stat.executeQuery(sql);     }    catch(Exception ex)    {       //ex.printStackTrace();     System.err.print("SQLException");    }     return rs;     }    //执行数据库更新     public void update(String sql)    {     try{       con = getCon();       stat = con.createStatement();       stat.executeUpdate(sql);     }    catch(Exception ex)    {       ex.printStackTrace();     }     }    //执行数据库更新     public void update(String sql,String[] args)    {     try{       con = getCon();     pstat = con.prepareStatement(sql);     for (int i=0;i<args.length;i++)    {        pstat.setString(i+1,args[i]);     }       pstat.executeUpdate();     }    catch(Exception ex)    {       ex.printStackTrace();     }     }    //关闭数据库连接    public void close()    {     try{     if (rs != null)rs.close();     if (stat != null)stat.close();       if (pstat != null)pstat.close();       if (con != null)con.close(); }    catch(Exception ex)    {       ex.printStackTrace();     }     } }

index.jsp
Java code
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %><%@ page import="java.io.*" %><jsp:useBean id="myDBbean" class="anhua.mysqlrs"></jsp:useBean><%!private static int Num;public void japInit(){    Num=0;    };%><!DOCTYPE HTML><html><head><meta charset="utf-8"><title>无标题文档</title></head><body><%="<br />欢迎<br />"%><% Num++; %><%="您是第"+Num+"个客人"%><Table border=2 bordercolor="#FFCCCC" align="center"><tr bgcolor=CCCCCC align=center>        <td><b>书号</b></td>    <td><b>书名</b></td>    <td><b>作者</b></td>    <td><b>出版社</b></td>    <td><b>出版社</b></td>    <td><b>出版社</b></td>    </tr><%ResultSet rs = myDBbean.query("select * from users");   while(rs.next()){%>      <tr align=center>        <td><%=rs.getString("id")%></td>     <td><%=rs.getString("uname")%></td>    <td><%=rs.getString("upass")%></td>    <td><%=rs.getString("phone")%></td>    <td><%=rs.getString("intime")%></td>    <td><%=rs.getString("ontime")%></td>   </tr>    <%}%>  </Table></body></html>


现在总是出错.我分析错误感觉连接数据库都没过去.
  相关解决方案