public static ResultSet queryData(String sql) throws SQLException{ Connection connection = JDBC(); PreparedStatement ps = connection.prepareStatement(sql); ResultSet rs = ps.executeQuery(); return rs; }检查代码并修改
时间: 2024-03-11 09:50:30 浏览: 151
在代码中,虽然正确地获取了数据库连接,但没有关闭连接和释放资源,建议在finally块中关闭连接和释放资源。同时,建议使用try-with-resources语句自动关闭资源,代码修改如下:
```
public static ResultSet queryData(String sql) throws SQLException {
try (Connection connection = JDBC();
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
return rs;
}
}
```
这样,当try语句块结束时,会自动关闭ResultSet、PreparedStatement和Connection对象,无需手动调用close()方法。
相关问题
package project; import java.sql.*; public class useOpea { public int query(String sql,Object[] objects) throws SQLException { int n=0; Connection conn=DriverManager.getConnection("jdbc:mysql://192.168.24.224:3306/dorm","root","123456"); PreparedStatement pstm=conn.prepareStatement(sql,1004,1007); if(objects!=null && objects.length>0){ for(int i=0;i<objects.length;i++ ) { pstm.setObject(i+1,objects[i]); } } ResultSet rs=pstm.executeQuery(); rs.last(); n=rs.getRow(); rs.close(); pstm.close(); conn.close(); return n; } public Object[][] queryToArray(String sql,Object[]pars) throws SQLException{ Connection conn=DriverManager.getConnection("jdbc:mysql://192.168.24.224:3306/dorm","root","123456"); PreparedStatement pstm=conn.prepareStatement(sql,1004,1007); if(pars!=null && pars.length>0){ for(int i=0;i<pars.length;i++ ) { pstm.setObject(i+1,pars[i]); } } ResultSet rs=pstm.executeQuery(); rs.last(); int n=rs.getRow(); ResultSetMetaData resultSetMetaData=rs.getMetaData(); int m=resultSetMetaData.getColumnCount(); Object[][]data=new Object[n][m]; rs.beforeFirst(); int i=0; while(rs.next()) { for(int j=0;j<m;j++) { data[i][j]=rs.getObject(j+1); i++; } } return data; } public static void main(String[]args) throws SQLException { useOpea userOpertion = new useOpea(); Object[][] data= userOpertion.queryToArray("select * from student", args); for(int i=0;i<data.length;i++) { for(int j=0;j<data[i].length;j++) { System.out.println(data[i][j]+" "); } } } }有什么错误
这段程序有一些潜在的问题,具体如下:
1. 在queryToArray方法中,如果查询结果集为空,即ResultSet.getRow()返回0,那么应该直接返回null,否则在后面的代码中会出现数组下标越界异常。
2. 在queryToArray方法中,内部循环的索引i应该累加在内层循环的末尾,否则会导致数据填充不完整。
3. 在main方法中,调用queryToArray方法时传入的参数args应该为null,否则会导致SQL语句解析错误。
4. 在main方法中,输出结果时应该使用print而不是println,否则会导致每个元素都换行输出。
综上所述,以下是修改后的程序代码:
```java
package project;
import java.sql.*;
public class useOpea {
public int query(String sql, Object[] objects) throws SQLException {
int n = 0;
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.24.224:3306/dorm", "root", "123456");
PreparedStatement pstm = conn.prepareStatement(sql, 1004, 1007);
if (objects != null && objects.length > 0) {
for (int i = 0; i < objects.length; i++) {
pstm.setObject(i + 1, objects[i]);
}
}
ResultSet rs = pstm.executeQuery();
rs.last();
n = rs.getRow();
rs.close();
pstm.close();
conn.close();
return n;
}
public Object[][] queryToArray(String sql, Object[] pars) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.24.224:3306/dorm", "root", "123456");
PreparedStatement pstm = conn.prepareStatement(sql, 1004, 1007);
if (pars != null && pars.length > 0) {
for (int i = 0; i < pars.length; i++) {
pstm.setObject(i + 1, pars[i]);
}
}
ResultSet rs = pstm.executeQuery();
rs.last();
int n = rs.getRow();
if (n == 0) {
return null;
}
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int m = resultSetMetaData.getColumnCount();
Object[][] data = new Object[n][m];
rs.beforeFirst();
int i = 0;
while (rs.next()) {
for (int j = 0; j < m; j++) {
data[i][j] = rs.getObject(j + 1);
}
i++;
}
return data;
}
public static void main(String[] args) throws SQLException {
useOpea userOpertion = new useOpea();
Object[][] data = userOpertion.queryToArray("select * from student", null);
if (data != null) {
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println();
}
}
}
}
```
javajdbc案例封装sql语言
在Java JDBC(Java Database Connectivity)案例中,为了简化数据库操作并避免直接拼接SQL语句带来的安全风险,通常会将SQL语句封装到DAO (Data Access Object) 或者Service层的方法中。以下是一个简单的例子:
```java
public class JdbcUtils {
private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
private static final String USER = "root";
private static final String PASS = "password";
// 使用连接池技术,如HikariCP、C3P0等
private static DataSource dataSource;
public JdbcUtils() {
if (dataSource == null) {
// 初始化数据源
dataSource = new HikariDataSource();
dataSource.setJdbcUrl(DB_URL);
dataSource.setUsername(USER);
dataSource.setPassword(PASS);
}
}
// 封装查询方法
public List<User> getUsers(String sqlQuery) throws SQLException {
try (Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sqlQuery)) {
pstmt.execute();
ResultSet rs = pstmt.getResultSet();
List<User> users = new ArrayList<>();
while (rs.next()) {
User user = new User(rs.getInt("id"), rs.getString("name"));
users.add(user);
}
return users;
}
}
}
// 用户类
class User {
int id;
String name;
// 构造器和getter/setter省略
}
```
在这个例子中,`getUsers`方法接收一个预编译的SQL查询字符串(如"SELECT * FROM users"),通过PreparedStatement来防止SQL注入攻击,并返回查询结果。这样的设计使得代码更易于维护和测试。
阅读全文