jdbc连接数据库的问题

*以下是代码:packagecom.jdbcTest.t09;importjava.sql.Connection;importjava.sql.DriverManager... *以下是代码:
package com.jdbcTest.t09;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MainTest {

public static void main(String[] args) {

try {
//1注册驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());

//2.建立连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/student","root","123456");

//3.创建statement,跟数据库打交道一定需要这个对象
Statement st = conn.createStatement();

// 4.执行查询
String sql ="select * from t_stu";
ResultSet rs = st.executeQuery(sql);

//5遍历查询每一条记录
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");

System.out.println("id"+id+"---name="+name+"---age="+age);

rs.close();
st.close();
conn.close();

}

} catch (SQLException e) {

e.printStackTrace();
}
}

}

//以下是运行错误信息:
Tue Sep 04 18:02:18 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
id1---name=zhangsan---age=18
java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:743)
at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6289)
at com.jdbcDemo.t09.MainTest.main(MainTest.java:32)

看得我眼花,求解
展开
 我来答
匿名用户
2018-09-04
展开全部
兄弟,你的这段
rs.close();
st.close();
conn.close();
关闭资源得放循环外面 嘛,不然都关闭连接了还怎么读取。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式