使用 ResultSet 遍历查询结果String getString(int columnIndex)

来源:互联网 发布:软件开发经费预算 编辑:程序博客网 时间:2024/06/01 09:52

使用 ResultSet  遍历查询结果

boolean next() 将光标从当前位置向前移一行。
String getString(int columnIndex) 以 Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列
的值。

eg:

package chap05.sec02;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import util.DbUtil;


public class Demo1 {
private static DbUtil dbUtil=new DbUtil();
public static void listBook() throws SQLException{
Connection con=dbUtil.getCon();
String sql="select * from t_book";
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
int id=rs.getInt(1);
String bookName=rs.getString(2);
float price=rs.getFloat(3);
String author=rs.getString(4);
int bookTypeId=rs.getInt(5);
System.out.println("图书编号:" + id + " 图书名称:" + bookName + " 图书价格:"
+ price + " 图书作者:" + author + " 图书类别id:" + bookTypeId);
System.out.println("****************************************");
}

}
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
listBook();
}


}

1 0
原创粉丝点击