问题描述
代码如下有其他方法和源码最好求大神指导Connectionconnection=null;PreparedStatementps=null;ResultSetMetaDatarsmd=null;ResultSetrs=null;try{Class.forName("com.mysql.jdbc.Driver");connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/coinfo","root","111111");ps=(PreparedStatement)connection.prepareStatement("select*fromrcd");rsmd=ps.getMetaData();rs=ps.executeQuery();for(inti=1;i<rsmd.getColumnCount()+1;i++){System.out.println(rsmd.getColumnName(i));while(rs.next()){System.out.println(rs.getString(rsmd.getColumnName(i)));}}}catch(ClassNotFoundExceptioncnfex){cnfex.printStackTrace();}catch(SQLExceptionsqlex){sqlex.printStackTrace();}
解决方案
解决方案二:
while(rs.next()){.放到for(inti=1;i<rsmd.getColumnCount()+1;i++)外面去
解决方案三:
executeQuery()是执行查询的方法吧??调试看看
解决方案四:
for和while调一下位置
解决方案五:
JAVA?大约是这样的:for(inti=1;i<rsmd.getColumnCount()+1;i++){System.out.println(rsmd.getColumnName(i));}while(rs.next()){for(inti=1;i<rsmd.getColumnCount()+1;i++){System.out.println(rs.getString(rsmd.getColumnName(i)));}}
话说你这个列下标是从1起的?从0起的话应该是for(inti=0;i<rsmd.getColumnCount();i++)
解决方案六:
for(inti=1;i<rsmd.getColumnCount()+1;i++){System.out.println(rsmd.getColumnName(i));while(rs.next()){System.out.println(rs.getString(rsmd.getColumnName(i)));}}while(rs.next()){System.out.println(rs.getString(rsmd.getColumnName(i)));}