java操作数据库取值问题
publicStringspeciesname(){Stringspeciesname=null;Connectionconnect=null;ResultSetresu...
public String speciesname()
{
String speciesname = null;
Connection connect = null;
ResultSet result_sql = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/seo", "root", "root");
Statement statement_read = connect.createStatement();
result_sql = statement_read.executeQuery("select * from new_species");
while(result_sql.next())
{
speciesname = result_sql.getString("species_name");
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
System.out.println(speciesname);
return speciesname;
}
我希望在while外面使用speciesname这个变量
求代码 展开
{
String speciesname = null;
Connection connect = null;
ResultSet result_sql = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/seo", "root", "root");
Statement statement_read = connect.createStatement();
result_sql = statement_read.executeQuery("select * from new_species");
while(result_sql.next())
{
speciesname = result_sql.getString("species_name");
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
System.out.println(speciesname);
return speciesname;
}
我希望在while外面使用speciesname这个变量
求代码 展开
展开全部
public List<String> speciesname()
{
String speciesname = null;
List<String> list = new ArrayList<String>(); //声明一个集合存放 speciesname
Connection connect = null;
ResultSet result_sql = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/seo", "root", "root");
Statement statement_read = connect.createStatement();
result_sql = statement_read.executeQuery("select * from new_species");
while(result_sql.next())
{
speciesname = result_sql.getString("species_name");
list.add(speciesname); //将每一个speciesname 放入集合
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
// System.out.println(speciesname);
// return speciesname;
/* for(String name:list){ //集合遍历,下一个For同样功能
System.out.println(name);
} */
for(int i=0; i < list.size(); i++){
System.out.println(list.get(i));
}
return list; 返回集合 可以在任何地方取出使用
}
{
String speciesname = null;
List<String> list = new ArrayList<String>(); //声明一个集合存放 speciesname
Connection connect = null;
ResultSet result_sql = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/seo", "root", "root");
Statement statement_read = connect.createStatement();
result_sql = statement_read.executeQuery("select * from new_species");
while(result_sql.next())
{
speciesname = result_sql.getString("species_name");
list.add(speciesname); //将每一个speciesname 放入集合
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
// System.out.println(speciesname);
// return speciesname;
/* for(String name:list){ //集合遍历,下一个For同样功能
System.out.println(name);
} */
for(int i=0; i < list.size(); i++){
System.out.println(list.get(i));
}
return list; 返回集合 可以在任何地方取出使用
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询