数据库用的是mysql,里面有个表temperature,表里有wd_1、wd_2、wd_3、、、、、、、wd_25、共25个字段,每个字段里面都有一个数值,有的可能为空,现在如何用循环来读取这些所有字段的值,读完以后加一块赋给wd.代码像下面这样:sql="select * from temperature"; //查询temperature表的SQL语句
rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
if(rs.next()){
wd=rs.getString("循环语句");不知道我这个思路对不对,就是把这25个字段的值全部读出来加一起变成一个数组,如果里面有空值的话,用null替代,如24,25,26,29,28,null,null~~
rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
if(rs.next()){
wd=rs.getString("循环语句");不知道我这个思路对不对,就是把这25个字段的值全部读出来加一起变成一个数组,如果里面有空值的话,用null替代,如24,25,26,29,28,null,null~~
wd1=rs.getString("wd_1"); //或者 wd1=rs.getString(1);
wd2=rs.getString("wd_2"); //或者 wd1=rs.getString(2);
wd3=rs.getString("wd_3");
wd4=rs.getString("wd_4"); //...............
wd25=rs.getString("wd_25");
}
直接select ((case when wd_1 is null then 0 else wd_1)+(case when wd_1 is null then 0 else wd_2)+(case when wd_1 is null then 0 else wd_3)...) as sumwd from temperature
省略号代表什么,你懂的
rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
String wd = "";
while (rs.next()){
for(int i =0;i<25;i++){
if(rs.getString(i)!= null && rs.getString(i).equals("")){
wd = wd + rs.getString(i);
}else{
wd = wd + "null";
}
}
}
System.out.println(wd);
sql="select * from temperature"; //查询temperature表的SQL语句
rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
String wd=""; //整点温度
while (rs.next()){
for(int i =0;i<25;i++){
if(rs.getString(i)!= null && !rs.getString(i).equals("")){ //这句报错:org.apache.jasper.JasperException: Exception in JSP: /main.jsp:68 wd = wd + rs.getString(i);
}else{
wd = wd + "null";
}
}
}
getInt
int getInt(int columnIndex)
throws SQLException以 Java 编程语言中 int 的形式检索此 ResultSet 对象的当前行中指定列的值。 参数:
columnIndex - 第一个列是 1,第二个列是 2,……
返回:
列值;如果值为 SQL NULL,则返回值为 0
throws SQLException {
DatabaseMetaData dbmd1 = conn.getMetaData();
ResultSet rs = null;
rs = dbmd1.getTables(null, null, "%", null); while (rs.next()) {
String name = rs.getString(3);
String type = rs.getString(4);
if (type.equals("TABLE")) {
System.out.println("table-name: " + name);
System.out.println("");
String sql = "select * from " + name;
// System.out.println("-----------:" + sql);
Statement stmt1 = conn.createStatement();
ResultSet rs1 = stmt1.executeQuery(sql);
ResultSetMetaData data = rs1.getMetaData();
for (int i = 1; i <= data.getColumnCount(); i++) {
// 获得所有列的数目及实际列数
// int columnCount=data.getColumnCount();
// 获得指定列的列名
String columnName = data.getColumnName(i);
System.out.println("column name :" + columnName);
// 获得指定列的数据类型名
String columnTypeName = data.getColumnTypeName(i);
System.out.println("column Type name :" + columnTypeName); }
System.out.println(""); }
}
这样写,可以直接获取记录,不需要再做任何循环操作,我觉得这样执行起来会快一些,个人愚见
Statement sta= con.createStatement();
ResultSet rs = sta.executeQuery("select * from temperature");
while(rs.next())
{
int sum = 0;
for (int i = 1; i <= 25; i++)
{
sum += rs.getInt(i);
}
System.out.println(sum);
}
sql="select * from temperature"; //查询温度的SQL语句
rs = stmt.executeQuery(sql);//执行查询,并返回查询结果
String wd="";//整点温度
if (rs.next())
{
for (int i = 1; i <= 25; i++)
{
if(i==1) { wd = rs.getString(i);//读取第一个字段是,字符串前面不需要加","号
}
else {wd = wd+","+ rs.getString(i);//从第二个字段开始,需要在新添加的字段前加","号
}
}
}
感谢楼上所有人对我的帮助!