当前位置: 技术问答>java相关
如何得到数据集得列数
来源: 互联网 发布时间:2015-07-25
本文导语: 我想得到数据集的列数,然后通过序号操作数据集,怎样得到数据集的列数? | 得到数据集的列数简单: Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); ResultSetMetaData rsmd ...
我想得到数据集的列数,然后通过序号操作数据集,怎样得到数据集的列数?
|
得到数据集的列数简单:
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
cols = rsmd.getColumnCount();
通过序号操作数据集不是很方便。
给你一个例子参考:
import java.io.*;
import java.util.*;
import java.sql.*;
public class JDBCBean {
String classname ; // JDBC driver class
String url ;
String username ;
String password ;
String query ; // the SQL statement.
int rows, cols; // the number of rows and columns of the result.
Vector result;
public void setClassname(String classname) {
this.classname = classname;
}
public String getClassname() {
return classname;
}
public void setUrl(/tech-qa-java/String url/index.html) {
this.url = url;
}
public String getUrl(){
return url;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setQuery(String query) {
this.query = query;
}
public String getQuery() {
return query;
}
protected void go()
throws ClassNotFoundException, SQLException
{
// Load the JDBC driver
Class.forName(classname);
// get the connection to the database
Connection con = DriverManager.getConnection(url, username,
password);
// create and execute the query
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
cols = rsmd.getColumnCount();
rows = 0;
result = new Vector();
String s[] = new String[cols];
for (int i=1; i
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
cols = rsmd.getColumnCount();
通过序号操作数据集不是很方便。
给你一个例子参考:
import java.io.*;
import java.util.*;
import java.sql.*;
public class JDBCBean {
String classname ; // JDBC driver class
String url ;
String username ;
String password ;
String query ; // the SQL statement.
int rows, cols; // the number of rows and columns of the result.
Vector result;
public void setClassname(String classname) {
this.classname = classname;
}
public String getClassname() {
return classname;
}
public void setUrl(/tech-qa-java/String url/index.html) {
this.url = url;
}
public String getUrl(){
return url;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setQuery(String query) {
this.query = query;
}
public String getQuery() {
return query;
}
protected void go()
throws ClassNotFoundException, SQLException
{
// Load the JDBC driver
Class.forName(classname);
// get the connection to the database
Connection con = DriverManager.getConnection(url, username,
password);
// create and execute the query
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
cols = rsmd.getColumnCount();
rows = 0;
result = new Vector();
String s[] = new String[cols];
for (int i=1; i