**
从一个普通的SQL语句中获取各个字段供其他属性使用
**
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ResultSet {
public static void main(String[] args) throws Exception {
//1、配置数据库,连接数据库
Connection connection = ConnDb2OrOrcl.getConnection(“com.ibm.db2.jcc.DB2Driver”, “jdbc:db2://127.0.0.1:50000/test”, “root”, “123”);
//2、用于执行静态SQL语句并返回其生成的结果的对象
Statement statement = connection.createStatement();
//3、静态SQL语句
String hb="select name,age,sex,school,class from test where age>=15 and age<=18";
//4、执行SQL语句, 获取结果集
ResultSet resultSet = statement.executeQuery(hb);
String name = null;
int age = 0;
String sex = null;
String school = null;
String class = null;
//5、使用while取出数据
while(resultSet.next()){
name = resultSet.getString(1);//获取该行的第1列
age = resultSet.getInt(2);//获取该行的第2列
sex = resultSet.getString(3);//获取该行的第3列
school = resultSet.getString(4);//获取该行的第4列
class = resultSet.getString(5);//获取该行的第5列
}
System.out.println(name + "\t" + age + "\t" + sex + "\t" + school + "\t" + class);
}
//6、关闭连接
resultSet.close();
statement.close();
connection.close();
}
}