当前位置: 代码迷 >> Java Web开发 >> 请问ResultSet中求列数用什么方法?
  详细解决方案

请问ResultSet中求列数用什么方法?

热度:275   发布时间:2006-09-09 20:44:15.0
请问ResultSet中求列数用什么方法?
求一个表中有多少列用什么方法?谢谢!
搜索更多相关主题的帖子: ResultSet  

----------------解决方案--------------------------------------------------------
count(*)
----------------解决方案--------------------------------------------------------

不是SQL里的count(*),今天老师用ResultSet写了一个方法,忘了记了,正在郁闷中...
我是想把所有行的列值放在Vector中用了个for循环,但是列的个数就不知道了.
----------------解决方案--------------------------------------------------------
哦哦,是列数,偶大意了。SORRY
不过偶不明白你在说什么,想要做什么


----------------解决方案--------------------------------------------------------

就是很简单,我要用一个表单输出数据表中的所有信息,列数是不确定的.那么要用个for循环把所有列值放入Vector中,那么这个列数怎么求?也就是for循环里的条件(???)!下面的cols
Vector data=new Vector();
int cols=???;
while(rs.next()){
for(int i=0;i<cols;i++){
data.add(rs.getArray(i));
}
}


----------------解决方案--------------------------------------------------------
哦,你这么一说就明白了
推荐一个相关教程
http://www.yesky.com/350/1813850.shtml

----------------解决方案--------------------------------------------------------
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
boolean b = rsmd.isSearchable(1);

----------------解决方案--------------------------------------------------------
谢谢各位,已经解决!
----------------解决方案--------------------------------------------------------

一道难题被自己亲手解决,真的是比洞房花烛夜还兴奋.
请允许我把代码贴上来.
//jspfindall.jsp
<%@ page contentType="text/html; charset=GBK" import="java.sql.*,java.util.*,jspfind.SqlVectorAll" %>
<html>
<head>
<title>
jspfindall
</title>
</head>
<body bgcolor="#ffffff">
<form action="" method="POST">
年龄:<input type="text" name="age" />
<input type="submit" value="查询" />
<table width="300" border="1">

<%
if(request.getParameter("age")==null){
return;
}
int age=Integer.parseInt(request.getParameter("age"));
SqlVectorAll sva=new SqlVectorAll();
Vector v=sva.getchaxun(age);
for(int i=0;i<v.size();i++){
%>
<tr>
<%
for(int j=0;j<((Vector)v.get(i)).size();j++){
%>
<td><%=((Vector)v.get(i)).get(j).toString()%></td>
<%}%>
</tr>
<%}%>
</table>
</form>
</body>
</html>

//

//SqlVectorAll.java
package jspfind;
import java.sql.*;
import java.util.*;

public class SqlVectorAll {
public SqlVectorAll() {
}
Connection con=null;
private Connection getcon(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(
"jdbc:odbc:driver=sql server;server=(local);database=test");
} catch (SQLException ex) {
System.out.println(ex);
} catch (ClassNotFoundException ex) {
}
return con;
}
public Vector getchaxun(int age){
Connection con=getcon();
Vector data = new Vector();
if(con==null){
return data;
}
Statement st=null;
String sql="select * from test where age >"+age;
try {
st = con.createStatement();
ResultSet rs=st.executeQuery(sql);
int cols=rs.getMetaData().getColumnCount();//获取列数
while(rs.next()){
Vector t= new Vector();
for(int i=1;i<=cols;i++){
t.add(rs.getString(i));
}
data.add(t);
}
} catch (SQLException ex) {
}
return data;
}
}
可能对大虾们这题算不了什么,但对于我这个JAVA残缺的人来说,真的是无与伦比.


----------------解决方案--------------------------------------------------------
  相关解决方案