首页 > 代码库 > JDBC中的ResultSet
JDBC中的ResultSet
ResultSet:结果集。存放数据库中的数据,可以将它想象成一张数据表,通过Statement对象中的executeQuery()方法获得
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.util.Properties;
/**
* Created by I am master on 2016/9/29.
*/
public class finallyjdbc {
public static void main(String[] args) throws Exception{
Connection connection=null;
Statement statement=null;
Driver driver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/student";
Properties properties = new Properties();
properties.put("user", "root");
properties.put("password", "root");
connection = driver.connect(url, properties);
statement = (Statement) connection.createStatement();
/*执行查询一条语句的操作*/
String query1 = "select * from studentInfo where id=1";
ResultSet resultSet1=statement.executeQuery(query1);
if(resultSet1.next()){
int id=resultSet1.getInt(1);
String name=resultSet1.getString("name");
int age=resultSet1.getInt(3);
String major=resultSet1.getString("class");
System.out.print(id);
System.out.print(name);
System.out.print(age);
System.out.println(major);
}
/*执行查询多条语句的操作*/
String query2 = "select * from studentInfo";
ResultSet resultSet2=statement.executeQuery(query2);
while (resultSet2.next()){
String id=resultSet2.getString("id");
String name=resultSet2.getString("name");
String age=resultSet2.getString("age");
String major=resultSet2.getString("class");
System.out.print(id);
System.out.print(name);
System.out.print(age);
System.out.println(major);
}
if(resultSet2!=null)
resultSet2.close();
if(resultSet1!=null)
resultSet1.close();
if(statement!=null)
statement.close();
if(connection!=null)
connection.close();
}
}
JDBC中的ResultSet
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。