代码报错 java.sql.SQLException: Column index out of range. at java.lang.Thread.run(Unknown Source)
就是想从mysql中取info表的数据,然后将其转成Json格式,最后通过Js调用json显示在页面,现在是转json格式的代码,求大神解答。在线等。。@Path("/h...
就是想从mysql中取info表的数据,然后将其转成Json格式,最后通过Js调用json显示在页面,现在是转json格式的代码,求大神解答。在线等。。
@Path("/hello")
public class HelloResource {
String[] Number = new String[10];
int i = 0;
String[] Name = new String[10];
String[] Age = new String[10];
@GET
@Produces(MediaType.TEXT_PLAIN)
public String Connect() throws JSONException, SQLException {
JSONArray array = new JSONArray();
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/liu";
String user = "root";
String password = "123";
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select id,username,age from info order by id desc";
ResultSet rs = statement.executeQuery(sql);
// 获取列数
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
while (rs.next()) {
JSONObject jsonObj = new JSONObject();
// 遍历每一列
for (i = 0; i < columnCount; i++) {
String columnName = metaData.getColumnLabel(i);
String value = rs.getString(columnName);
jsonObj.put(columnName, value);
}
array.add(jsonObj);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return array.toString();
}
} 展开
@Path("/hello")
public class HelloResource {
String[] Number = new String[10];
int i = 0;
String[] Name = new String[10];
String[] Age = new String[10];
@GET
@Produces(MediaType.TEXT_PLAIN)
public String Connect() throws JSONException, SQLException {
JSONArray array = new JSONArray();
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/liu";
String user = "root";
String password = "123";
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select id,username,age from info order by id desc";
ResultSet rs = statement.executeQuery(sql);
// 获取列数
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
while (rs.next()) {
JSONObject jsonObj = new JSONObject();
// 遍历每一列
for (i = 0; i < columnCount; i++) {
String columnName = metaData.getColumnLabel(i);
String value = rs.getString(columnName);
jsonObj.put(columnName, value);
}
array.add(jsonObj);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return array.toString();
}
} 展开
2个回答
展开全部
java.sql.SQLException: Column index out of range可知很可能是String columnName = metaData.getColumnLabel(i);
一句越界了,那说明ResultSetMetaData
不能取第0个,可能的原因有两个,一个是里面没有元素,另一个是下标从1开始,所以:
for (i = 0; i < columnCount; i++) 改成 for (i = 1; i <= columnCount; i++) 试试吧
一句越界了,那说明ResultSetMetaData
不能取第0个,可能的原因有两个,一个是里面没有元素,另一个是下标从1开始,所以:
for (i = 0; i < columnCount; i++) 改成 for (i = 1; i <= columnCount; i++) 试试吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询