JAVA 关于数据库select问题
packagecom.student;importcom.student.DatabaseExecute;publicclassAextendsDatabaseExecu...
package com.student;
import com.student.DatabaseExecute;
public class A extends DatabaseExecute
{
void start()
{
//insert("insert into userinfo (name,age,sex) values ('leo',22,'a')");
java.util.Vector v = select("select * from userinfo");
for(int i=0;i<v.size();i++)
{
System.out.println(v.get(i));
}
}
public static void main(String[] args)
{
new A().start();
}
}
----------------------------------------------------------------
package com.student;
import java.sql.*;
import java.util.Vector;
public class DatabaseExecute
{
private Connection con;
private Statement stat;
private ResultSet rs;
private void connection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:sourceName", "", "");
stat = con.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Vector select(String selectStr)
{
connection();
try
{
Vector v = new Vector();
rs=stat.executeQuery(selectStr);
while(rs.next())
{
v.add(rs.getString(1));//这里怎么写能让它显示出全部?而不是现在这样单独显示1列
}
rs.close();
return v;
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try {
if(stat!=null)
stat.close();
if(con!=null)
con.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return null;
}
}
----------------------------------------------------------------
v.add(rs.getString(1));//这里怎么写能让它显示出全部?而不是现在这样单独显示1列 展开
import com.student.DatabaseExecute;
public class A extends DatabaseExecute
{
void start()
{
//insert("insert into userinfo (name,age,sex) values ('leo',22,'a')");
java.util.Vector v = select("select * from userinfo");
for(int i=0;i<v.size();i++)
{
System.out.println(v.get(i));
}
}
public static void main(String[] args)
{
new A().start();
}
}
----------------------------------------------------------------
package com.student;
import java.sql.*;
import java.util.Vector;
public class DatabaseExecute
{
private Connection con;
private Statement stat;
private ResultSet rs;
private void connection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:sourceName", "", "");
stat = con.createStatement();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Vector select(String selectStr)
{
connection();
try
{
Vector v = new Vector();
rs=stat.executeQuery(selectStr);
while(rs.next())
{
v.add(rs.getString(1));//这里怎么写能让它显示出全部?而不是现在这样单独显示1列
}
rs.close();
return v;
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try {
if(stat!=null)
stat.close();
if(con!=null)
con.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return null;
}
}
----------------------------------------------------------------
v.add(rs.getString(1));//这里怎么写能让它显示出全部?而不是现在这样单独显示1列 展开
1个回答
展开全部
老兄,刚给你改了上面的那个题!这里和那里一样的!如果还有什么问题可以加我QQ帮你解决 252956244
public Vector select(String selectStr)
{
connection();
try
{
Vector v = new Vector();
rs=stat.executeQuery(selectStr);
ResultSetMetaData rsmd=rs.getMetaData();
int colCount = rsmd.getColumnCount();
while(rs.next())
for (int i=0;i<colCount;i++){
{
v.add(rs.getString(i+1)); //这里要这样做
}
}
rs.close();
return v;
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
......
}
public Vector select(String selectStr)
{
connection();
try
{
Vector v = new Vector();
rs=stat.executeQuery(selectStr);
ResultSetMetaData rsmd=rs.getMetaData();
int colCount = rsmd.getColumnCount();
while(rs.next())
for (int i=0;i<colCount;i++){
{
v.add(rs.getString(i+1)); //这里要这样做
}
}
rs.close();
return v;
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
......
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询