Java 从ArrayList到字符串[]
我实现了一个方法,该方法返回SQL数据库上的查询结果。 我只希望该方法只返回一个字符串[],它是在db上选择列的查询的结果。 这是我的代码:Java 从ArrayList到字符串[],java,sql,arraylist,Java,Sql,Arraylist,我实现了一个方法,该方法返回SQL数据库上的查询结果。 我只希望该方法只返回一个字符串[],它是在db上选择列的查询的结果。 这是我的代码: public class DBConnection { private static Connection con; private static Statement st; private static ResultSet rs; try { Class.forName("com.mysql.jdbc.Driver
public class DBConnection {
private static Connection con;
private static Statement st;
private static ResultSet rs;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","user","password");
st = con.createStatement();
}
catch (Exception ex)
{
System.out.println("Error: "+ex);
}
public ArrayList<String[]> doQuery (String query)
{
ArrayList<String[]> v = null;
String [] record;
int columns = 0;
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
v = new ArrayList<String[]>();
ResultSetMetaData rsmd = rs.getMetaData();
columns= rsmd.getColumnCount();
while(rs.next()) {
record = new String[columns];
for (int i=0; i<colonne; i++) record[i] = rs.getString(i+1);
v.add( (String[]) record.clone() );
}
rs.close();
stmt.close();
} catch (Exception e) { e.printStackTrace(); }
return v;
}
公共类数据库连接{
专用静态连接;
私有静态语句st;
私有静态结果集;
尝试
{
Class.forName(“com.mysql.jdbc.Driver”);
con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/database“,”用户“,”密码“);
st=con.createStatement();
}
捕获(例外情况除外)
{
System.out.println(“错误:+ex”);
}
公共ArrayList doQuery(字符串查询)
{
ArrayList v=null;
字符串[]记录;
int列=0;
试一试{
语句stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(查询);
v=新的ArrayList();
ResultSetMetaData rsmd=rs.getMetaData();
columns=rsmd.getColumnCount();
while(rs.next()){
记录=新字符串[列];
对于(int i=0;i为什么不调用v.toArray(新字符串[0])
?为什么不调用v.toArray(新字符串[0])
?粘贴链接中给出的解决方案
ArrayList list=new ArrayList();
字符串[]数组=list.toArray(新字符串[list.size()]);
(或)
ArrayList ArrayList=new ArrayList();
Object[]ObjectList=arrayList.toArray();
String[]StringArray=Arrays.copyof(ObjectList,ObjectList.length,String[].class);
粘贴链接中给出的解决方案
ArrayList list=new ArrayList();
字符串[]数组=list.toArray(新字符串[list.size()]);
(或)
ArrayList ArrayList=new ArrayList();
Object[]ObjectList=arrayList.toArray();
String[]StringArray=Arrays.copyof(ObjectList,ObjectList.length,String[].class);
我假设您的问题有两个部分:a)您想返回字符串数组,b)您想只返回一列
a)的答案已经给出或至少暗示了。
b)的答案要求您知道要返回或调整查询的列的名称
您可以将方法更改为以下内容:
public String[] doQuery (String query, String columnName) //columnName not needed if you know the index of the column or if the name is always the same, in which case it could be some constant
{
List<String> v = new ArrayList<String>();
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
v.add( rs.getString(columnName) ); //or rs.getString(1); if you know the column is the first in the query's result
}
rs.close();
stmt.close();
} catch (Exception e) { e.printStackTrace(); }
return v.toArray(new String[v.size()]);
}
// create an ArrayList
ArrayList<String> theArrayList = new ArrayList<String>();
theArrayList.add("aString");
theArrayList.add("anotherString");
// get its contents as an Array
String[] theArray = new String[theArrayList .size()];
theArray = theArrayList.toArray(theArray);
public String[]doQuery(String query,String columnName)//如果知道列的索引或名称始终相同,则不需要columnName,在这种情况下,它可以是某个常量
{
列表v=新的ArrayList();
试一试{
语句stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(查询);
while(rs.next()){
v、 添加(rs.getString(columnName));//或rs.getString(1);如果您知道该列是查询结果中的第一列
}
rs.close();
stmt.close();
}catch(异常e){e.printStackTrace();}
返回v.toArray(新字符串[v.size()]);
}
请注意:
- 您必须确保列具有您要查询的名称,即,您不能执行
从…
中选择columnA,然后调用rs.getString(“columnB”);
。如果您不知道名称,但知道结果集中列的索引,请使用rs.getString(x);
相反,其中x
是基于一的索引
- 您也可以使用
v.toArray(新字符串[v.size()]);
而不是v.toArray(新字符串[0]);
。两者之间的区别在于前者返回作为参数传递的数组,而后者在内部创建一个新数组并返回该数组
我假设您的问题有两个部分:a)您希望返回字符串数组,b)您希望只返回单个列
a)的答案已经给出或至少暗示了。
b)的答案要求您知道要返回或调整查询的列的名称
您可以将方法更改为以下内容:
public String[] doQuery (String query, String columnName) //columnName not needed if you know the index of the column or if the name is always the same, in which case it could be some constant
{
List<String> v = new ArrayList<String>();
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
v.add( rs.getString(columnName) ); //or rs.getString(1); if you know the column is the first in the query's result
}
rs.close();
stmt.close();
} catch (Exception e) { e.printStackTrace(); }
return v.toArray(new String[v.size()]);
}
// create an ArrayList
ArrayList<String> theArrayList = new ArrayList<String>();
theArrayList.add("aString");
theArrayList.add("anotherString");
// get its contents as an Array
String[] theArray = new String[theArrayList .size()];
theArray = theArrayList.toArray(theArray);
public String[]doQuery(String query,String columnName)//如果知道列的索引或名称始终相同,则不需要columnName,在这种情况下,它可以是某个常量
{
列表v=新的ArrayList();
试一试{
语句stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(查询);
while(rs.next()){
v、 添加(rs.getString(columnName));//或rs.getString(1);如果您知道该列是查询结果中的第一列
}
rs.close();
stmt.close();
}catch(异常e){e.printStackTrace();}
返回v.toArray(新字符串[v.size()]);
}
请注意:
- 您必须确保列具有您要查询的名称,即,您不能执行
从…
中选择columnA,然后调用rs.getString(“columnB”);
。如果您不知道名称,但知道结果集中列的索引,请使用rs.getString(x);
相反,其中x
是基于一的索引
- 您也可以使用
v.toArray(新字符串[v.size()]);
而不是v.toArray(新字符串[0]);
。两者之间的区别在于前者返回作为参数传递的数组,而后者在内部创建一个新数组并返回该数组
在ArrayList上,可以调用toArray()来获取其值的数组
这看起来像这样:
public String[] doQuery (String query, String columnName) //columnName not needed if you know the index of the column or if the name is always the same, in which case it could be some constant
{
List<String> v = new ArrayList<String>();
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
v.add( rs.getString(columnName) ); //or rs.getString(1); if you know the column is the first in the query's result
}
rs.close();
stmt.close();
} catch (Exception e) { e.printStackTrace(); }
return v.toArray(new String[v.size()]);
}
// create an ArrayList
ArrayList<String> theArrayList = new ArrayList<String>();
theArrayList.add("aString");
theArrayList.add("anotherString");
// get its contents as an Array
String[] theArray = new String[theArrayList .size()];
theArray = theArrayList.toArray(theArray);
//创建一个ArrayList
ArrayList theArrayList=新的ArrayList();
ArrayList.添加(“aString”);
arraylist.add(“另一个字符串”);
//以数组形式获取其内容
String[]theArray=新字符串[theArrayList.size()];
theArray=theArrayList.toArray(theArray);
您可以在.中的ArrayList中查找更多详细信息,您可以调用toArray()以获取其值的数组
这看起来像这样:
public String[] doQuery (String query, String columnName) //columnName not needed if you know the index of the column or if the name is always the same, in which case it could be some constant
{
List<String> v = new ArrayList<String>();
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
v.add( rs.getString(columnName) ); //or rs.getString(1); if you know the column is the first in the query's result
}
rs.close();
stmt.close();
} catch (Exception e) { e.printStackTrace(); }
return v.toArray(new String[v.size()]);
}
// create an ArrayList
ArrayList<String> theArrayList = new ArrayList<String>();
theArrayList.add("aString");
theArrayList.add("anotherString");
// get its contents as an Array
String[] theArray = new String[theArrayList .size()];
theArray = theArrayList.toArray(theArray);
//创建一个ArrayList
ArrayList theArrayList=新的ArrayList();
ArrayList.添加(“aString”);
阵列