在线等,关于通过servlet提取数据库的数据,然后通过JSP页面显示出来
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServl...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
String indexmsg=request.getParameter("msg");
if(indexmsg!=null)
{
try
{
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
out.print("Success loading Mysql Driver!");
}
catch (Exception e)
{
out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try
{
Connection connect = DriverManager.getConnection( //连接数据库
"jdbc:mysql://localhost:3306/eclipse","root","026499");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
out.print("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from msg");
//msg 为你表的名称
List<msg> list=new ArrayList<msg>(); //创建一个表,用来暂时存储从数据库拿出来的数据
while (rs.next())
{
msg m=new msg();
m.Setname(rs.getString("userName")); //把数据库的数据放入表中(在JSP页面通过提取这个表中的数据来完成数据库
m.Setpass(rs.getString("pass")); // 中数据的传递
list.add(m);
request.setAttribute("mysqlmsg", list);
}
request.getRequestDispatcher("msgplay.jsp").forward(request, response);
}
catch (Exception e)
{
System.out.print("get data error!");
e.printStackTrace();
}
}
else
{
request.getRequestDispatcher("index1.jsp").forward(request, response);
}
以下是提示的错误:
严重: Servlet.service() for servlet [jsp] threw exception
java.lang.ClassCastException: java.util.ArrayList cannot be cast to infopros.msg
at org.apache.jsp.msgplay_jsp._jspService(msgplay_jsp.java:126)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at 展开
{
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
String indexmsg=request.getParameter("msg");
if(indexmsg!=null)
{
try
{
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
out.print("Success loading Mysql Driver!");
}
catch (Exception e)
{
out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try
{
Connection connect = DriverManager.getConnection( //连接数据库
"jdbc:mysql://localhost:3306/eclipse","root","026499");
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
out.print("Success connect Mysql server!");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from msg");
//msg 为你表的名称
List<msg> list=new ArrayList<msg>(); //创建一个表,用来暂时存储从数据库拿出来的数据
while (rs.next())
{
msg m=new msg();
m.Setname(rs.getString("userName")); //把数据库的数据放入表中(在JSP页面通过提取这个表中的数据来完成数据库
m.Setpass(rs.getString("pass")); // 中数据的传递
list.add(m);
request.setAttribute("mysqlmsg", list);
}
request.getRequestDispatcher("msgplay.jsp").forward(request, response);
}
catch (Exception e)
{
System.out.print("get data error!");
e.printStackTrace();
}
}
else
{
request.getRequestDispatcher("index1.jsp").forward(request, response);
}
以下是提示的错误:
严重: Servlet.service() for servlet [jsp] threw exception
java.lang.ClassCastException: java.util.ArrayList cannot be cast to infopros.msg
at org.apache.jsp.msgplay_jsp._jspService(msgplay_jsp.java:126)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询