怎么用Java将sql数据库里的表格展示在平台上?
publicclassUserControllerextendsBaseControl{privatestaticfinalLoggerlogger=LoggerFact...
public class UserController extends BaseControl {
private static final Logger logger=LoggerFactory.getLogger(UserController.class);
private UserService userService;
private final static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=RUNOOB";
private static final String USER="sa";
private static final String PASSWORD="1234561";
public void setUserService(UserService userService) {
this.userService = userService;
}
public ModelAndView NewFile(HttpServletRequest request,HttpServletResponse response) {
logger.debug("NewFile(HttpServletRequest request,HttpServletResponse response) ");
//父表下拉菜单
return new ModelAndView("app/train/user/NewFile");
}
public ModelAndView getdata(HttpServletRequest request,HttpServletResponse response) throws SQLException {
logger.debug("NewFile(HttpServletRequest request,HttpServletResponse response) ");
Connection conn=null;
try {
//1.加载驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.获得数据库的连接
conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
Statement stmt = conn.createStatement();
。
ResultSet rs = stmt.executeQuery("select id,name,url,alexa,country from Websites");
String s = "";
while(rs.next()){//如果对象中有数据,就会循环打印出来
System.out.println(rs.getInt("id")+","+rs.getString("name")+","+rs.getString("url")+","+rs.getString("alexa")+","+rs.getString("country"));
s += rs.getString("name") + ",";
}
super.outJson(s,response);
return null;
}
我现在这样弄的,只能输出一行"name",我现在想将这5行数据都输出,怎么弄? 展开
private static final Logger logger=LoggerFactory.getLogger(UserController.class);
private UserService userService;
private final static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=RUNOOB";
private static final String USER="sa";
private static final String PASSWORD="1234561";
public void setUserService(UserService userService) {
this.userService = userService;
}
public ModelAndView NewFile(HttpServletRequest request,HttpServletResponse response) {
logger.debug("NewFile(HttpServletRequest request,HttpServletResponse response) ");
//父表下拉菜单
return new ModelAndView("app/train/user/NewFile");
}
public ModelAndView getdata(HttpServletRequest request,HttpServletResponse response) throws SQLException {
logger.debug("NewFile(HttpServletRequest request,HttpServletResponse response) ");
Connection conn=null;
try {
//1.加载驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.获得数据库的连接
conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
Statement stmt = conn.createStatement();
。
ResultSet rs = stmt.executeQuery("select id,name,url,alexa,country from Websites");
String s = "";
while(rs.next()){//如果对象中有数据,就会循环打印出来
System.out.println(rs.getInt("id")+","+rs.getString("name")+","+rs.getString("url")+","+rs.getString("alexa")+","+rs.getString("country"));
s += rs.getString("name") + ",";
}
super.outJson(s,response);
return null;
}
我现在这样弄的,只能输出一行"name",我现在想将这5行数据都输出,怎么弄? 展开
2个回答
展开全部
这个要从头说起就复杂了。我简单说一下吧。
首先,要把数据从库中取出来,假设你已经会了。
其次,创建一个表格模型,
DefaultTableModel model=new DefaultTableModel();
//创建表头
model.setColumnIdentifiers(new Object[]{"日期/时间","操作工","产品型号","数量"});
再次,添加数据
while(results.next())
{
String dt=results.getString("dt");
String name=results.getString("name");
String pname=results.getString("product");
int i=results.getInt("num");
//把以上数据添加到表格模型的一行中
model.addRow(new Object[]{dt,name,pname,i});
}
最后,用模型生成表格
JTable table=new JTable(model);
当然,还有一些其它的操作,比如把表格显示出来,表格的字体、行高等设置等等,不再一一赘述。
首先,要把数据从库中取出来,假设你已经会了。
其次,创建一个表格模型,
DefaultTableModel model=new DefaultTableModel();
//创建表头
model.setColumnIdentifiers(new Object[]{"日期/时间","操作工","产品型号","数量"});
再次,添加数据
while(results.next())
{
String dt=results.getString("dt");
String name=results.getString("name");
String pname=results.getString("product");
int i=results.getInt("num");
//把以上数据添加到表格模型的一行中
model.addRow(new Object[]{dt,name,pname,i});
}
最后,用模型生成表格
JTable table=new JTable(model);
当然,还有一些其它的操作,比如把表格显示出来,表格的字体、行高等设置等等,不再一一赘述。
展开全部
修改下while循环即可
s = rs.getInt("id")+","+rs.getString("name")+","+rs.getString("url")+","+rs.getString("alexa")+","+rs.getString("country");
更多追问追答
追问
对啊,我知道修改一下while就行,但是怎么修改呢?我试着修改了一下,报错。要求使用map,你这种方法不行。
追答
下面这段代码是你之前写的
ResultSet rs = stmt.executeQuery("select id,name,url,alexa,country from Websites");
String s = "";
while(rs.next()){//如果对象中有数据,就会循环打印出来
System.out.println(rs.getInt("id")+","+rs.getString("name")+","+rs.getString("url")+","+rs.getString("alexa")+","+rs.getString("country"));
s += rs.getString("name") + ",";
}
super.outJson(s,response);
请修改成下面的代码
ResultSet rs = stmt.executeQuery("select id,name,url,alexa,country from Websites");
List<String> list = new ArrayList<>();
String s = "";
while(rs.next()){
s = rs.getInt("id")+","+rs.getString("name")+","+rs.getString("url")+","+rs.getString("alexa")+","+rs.getString("country"));
list.add(s);
}
//list中每一个元素就是id,name,url这样的形式,有多少个list中就有多少个
super.outJson(list,response);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |