JavaWeb基础知识总结. (39)

请注册,执行查询使用的不是executeUpdate()方法,而是executeQuery()方法。executeQuery()方法返回的是ResultSet,ResultSet封装了查询结果,我们称之为结果集。

4.6 读取结果集中的数据

ResultSet就是一张二维的表格,它内部有一个“行光标”,光标默认的位置在“第一行上方”,我们可以调用rs对象的next()方法把“行光标”向下移动一行,当第一次调用next()方法时,“行光标”就到了第一行记录的位置,这时就可以使用ResultSet提供的getXXX(int col)方法来获取指定列的数据了:

rs.next();//光标移动到第一行

rs.getInt(1);//获取第一行第一列的数据

当你使用rs.getInt(1)方法时,你必须可以肯定第1列的数据类型就是int类型,如果你不能肯定,那么最好使用rs.getObject(1)。在ResultSet类中提供了一系列的getXXX()方法,比较常用的方法有:

Object getObject(int col)

String getString(int col)

int getInt(int col)

double getDouble(int col)

4.7 关闭

与IO流一样,使用后的东西都需要关闭!关闭的顺序是先得到的后关闭,后得到的先关闭。

rs.close();

stmt.close();

con.close();

4.8 代码

public static Connection getConnection() throws Exception {

Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/mydb1";

return DriverManager.getConnection(url, "root", "123");

}

 

@Test

public void insert() throws Exception {

Connection con = getConnection();

Statement stmt = con.createStatement();

String sql = "insert into user values(\'zhangSan\', \'123\')";

stmt.executeUpdate(sql);

System.out.println("插入成功!");

}

 

@Test

public void update() throws Exception {

Connection con = getConnection();

Statement stmt = con.createStatement();

String sql = "update user set password=\'456\' where username=\'zhangSan\'";

stmt.executeUpdate(sql);

System.out.println("修改成功!");

}

 

@Test

public void delete() throws Exception {

Connection con = getConnection();

Statement stmt = con.createStatement();

String sql = "delete from user where username=\'zhangSan\'";

stmt.executeUpdate(sql);

System.out.println("删除成功!");

}

 

@Test

public void query() throws Exception {

Connection con = getConnection();

Statement stmt = con.createStatement();

String sql = "select * from user";

ResultSet rs = stmt.executeQuery(sql);

while(rs.next()) {

String username = rs.getString(1);

String password = rs.getString(2);

System.out.println(username + ", " + password);

}

}

 
4.9 规范化代码

所谓规范化代码就是无论是否出现异常,都要关闭ResultSet、Statement,以及Connection,如果你还记得IO流的规范化代码,那么下面的代码你就明白什么意思了。

@Test

public void query() {

Connection con = null;

Statement stmt = null;

ResultSet rs = null;

try {

con = getConnection();

stmt = con.createStatement();

String sql = "select * from user";

rs = stmt.executeQuery(sql);

while(rs.next()) {

String username = rs.getString(1);

String password = rs.getString(2);

System.out.println(username + ", " + password);

}

catch(Exception e) {

throw new RuntimeException(e);

finally {

try {

if(rs != null) rs.close();

if(stmt != null) stmt.close();

if(con != null) con.close();

catch(SQLException e) {}

}

}

 
JDBC对象介绍 1 JDBC中的主要类(接口)

在JDBC中常用的类有:

l DriverManager;

l Connection;

l Statement;

l ResultSet。

2 DriverManager

其实我们今后只需要会用DriverManager的getConnection()方法即可:

1. Class.forName(“com.mysql.jdbc.Driver”);//注册驱动

2. String url = “jdbc:mysql://localhost:3306/mydb1”;

3. String username = “root”;

4. String password = “123”;

5. Connection con = DriverManager.getConnection(url, username, password);

注意,上面代码可能出现的两种异常:

1. ClassNotFoundException:这个异常是在第1句上出现的,出现这个异常有两个可能:

l 你没有给出mysql的jar包;

l 你把类名称打错了,查看类名是不是com.mysql.jdbc.Driver。

2. SQLException:这个异常出现在第5句,出现这个异常就是三个参数的问题,往往username和password一般不是出错,所以需要认真查看url是否打错。

对于DriverManager.registerDriver()方法了解即可,因为我们今后注册驱动只会Class.forName(),而不会使用这个方法。

3 Connection

Connection最为重要的方法就是获取Statement:

l Statement stmt = con.createStatement();

后面在学习ResultSet方法时,还要学习一下下面的方法:

l Statement stmt = con.createStatement(int,int);

4 Statement

Statement最为重要的方法是:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwzfdw.html