java中如何遍历字符串二维数组?
初学java,老师让我们用遍历两边的方法,下面是我的代码,可是总是报空指针异常,已知数组为9列//查询数据库中所有用户信息的方法publicString[][]querr...
初学java,老师让我们用遍历两边的方法,下面是我的代码,可是总是报空指针异常,已知数组为9列
//查询数据库中所有用户信息的方法
public String[][] querryAll() {
String querry = "select * from tb_user";
DBManager dbm = null;
Connection conn = null;
Statement sta = null;
ResultSet rs= null;
conn = manager.getConnection();
try {
dbm = new DBManager();
conn = dbm.getConnection();
sta = conn.createStatement();
rs = sta.executeQuery(querry);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//rs需先遍历一遍,获得总行数
int n = 0;
try {
while(rs.next()) {
n++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//遍历第二遍,将结果放进二维数组
try {
dbm = new DBManager();
conn = dbm.getConnection();
sta = conn.createStatement();
rs = sta.executeQuery(querry);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[][] temp = null ;
try {
while(rs.next()) {
for (int i = 0; i < n; i++) {
temp[i][9] = rs.getString(i);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return temp;
} 展开
//查询数据库中所有用户信息的方法
public String[][] querryAll() {
String querry = "select * from tb_user";
DBManager dbm = null;
Connection conn = null;
Statement sta = null;
ResultSet rs= null;
conn = manager.getConnection();
try {
dbm = new DBManager();
conn = dbm.getConnection();
sta = conn.createStatement();
rs = sta.executeQuery(querry);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//rs需先遍历一遍,获得总行数
int n = 0;
try {
while(rs.next()) {
n++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//遍历第二遍,将结果放进二维数组
try {
dbm = new DBManager();
conn = dbm.getConnection();
sta = conn.createStatement();
rs = sta.executeQuery(querry);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[][] temp = null ;
try {
while(rs.next()) {
for (int i = 0; i < n; i++) {
temp[i][9] = rs.getString(i);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return temp;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询