菜鸟求:Java程序读取数据库表项并显示在页面上,该如何做?

cyqcyq2008 2013-03-04 07:39:57
本人是Java菜鸟一枚,现有两个小程序:1、从网页端通过提交到servlet服务器的若干参数(如org_name、mapno等),show_pathway.java作为后台服务器接收参数。2、连接数据库mysql并将表单数据显示出来的java程序。如何将两个小程序融合到一起作为后台处理程序。我不太清楚java结构以及内部如何调用,因为不能机械的和在一起。两个程序的代码如下:1、

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class show_pathway extends HttpServlet {
public void service(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String org_name = request.getParameter("org_name");
out.println(org_name);
String mapno = request.getParameter("mapno");
out.println(mapno);
}
}

2、

import java.sql.*;

public class JDBCTest {
public static void main(String[] args){

String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
String user = "root";
String password = "";
System.out.println("执行结果如下所示:");
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");

Statement statement = conn.createStatement();
String sql = "select * from student";
ResultSet rs = statement.executeQuery(sql);

System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 学号" + "\t" + " 姓名");
System.out.println("-----------------");

String name = null;

while(rs.next()) {

name = rs.getString("sname");

name = new String(name.getBytes("ISO-8859-1"),"GB2312");

System.out.println(rs.getString("sno") + "\t" + name);
}

rs.close();
conn.close();

} catch(ClassNotFoundException e) {

System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();

} catch(SQLException e) {

e.printStackTrace();

} catch(Exception e) {

e.printStackTrace();

}
}
}


请大神指点!!!
...全文
218 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ao_xun 2013-03-07
  • 打赏
  • 举报
回复
读取数据,并保存在一个对象里面,如果是多个对象,则保存在容器中。通过request.setAttribute()保存到HttpRequest对象里面并且进行跳转到你指定的jsp页面。由于jsp默认对象里面有request对象,所以你在取出来,循环遍历就行了~~

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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