import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCDEMO1 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接对象
Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/mybase", "root", "root");
//3.获取执行SQL 语句对象
Statement stat = con.createStatement();
//拼写查询的sql
String sql = "SELECT * FROM sort";
//调用执行者对象的方法,executeQuery(String sql) 执行SQL语句中的select查询
//返回值ResultSet接口的实现类对象,实现类在mysql驱动中
ResultSet rs = stat.executeQuery(sql);
//5.处理结果集
//ResultSet接口方法boolean next() 返回true,有结果集,返回false没有结果集
while(rs.next()) {
//获取每列数据,使用ReasultSet接口的方法getXX
System.out .println(rs.getInt("sid")
+ " " + rs.getString("sname") + rs.getDouble("sprice") + rs.getString("sdesc"));
//System.out .println(rs.getObject("sid")
//+ " " + rs.getObject("sname") + rs.getObject("sprice") + //rs.getObject("sdesc"));
rs.close();
stat.close();
con.close();
}
}
}