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]+" "); } } } }有什么错误
时间: 2024-03-08 07:48:22 浏览: 159
这段程序有一些潜在的问题,具体如下:
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();
}
}
}
}
```
阅读全文