在java中循环之外访问字符串变量? [英] Access string variable outside for loop in java?
本文介绍了在java中循环之外访问字符串变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Coding :
int row = table.getRowCount();
int column = table.getColumnCount();
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
//System.out.println(table.getValueAt(j, i));
String readstr = (String) table.getValueAt(j, i); // I WANT TO ACCESS THIS STRING
}
}
try{
// FILE READ AND TABLE CREATE
String tname="example";
String dbname="DB123";
Class.forName("com.mysql.jdbc.Driver");
Connection conn= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/DB123","root","");
Statement stmt=(Statement) conn.createStatement();
System.out.println("connect");
DatabaseMetaData dmd = (DatabaseMetaData) conn.getMetaData();
while((readstr=brr.readLine())!=null) // TO USE IT HERE
{
ResultSet rs = dmd.getTables(null,"MigrationDB", "example", null);
//set not null column
if (String.valueOf(readstr).contains("NO"))
readstr=readstr.replaceAll("NO", "not null");
else if(String.valueOf(readstr).contains("YES"))
readstr=readstr.replaceAll("YES", "");
if(String.valueOf(readstr).contains("PRIMARY"))
readstr=readstr.replaceFirst("PRIMARY","primary key");
System.out.println("replace string "+readstr);
int k=1;
if (!rs.next())
{
stmt.executeUpdate("CREATE TABLE "+tname+ "("+readstr+")");
}
else{
System.out.println(readstr);
stmt.executeUpdate("ALTER TABLE "+tname+ " ADD("+readstr+")");
System.out.println("altered");
}
}
}
catch (Exception e){}
}
}
谢谢.
解决方案
您需要了解什么是局部变量,以及它们的生存期是多少.我建议您通读本教程: http://www.tutorialspoint.com/java/java_variable_types.htm
在这种情况下,只需要在for循环外声明readstr即可.
String readstr;
for(//loop condition) {
readstr = read value ;
}
hi folks i want to access a string variable outside for loop, so that i can use it for further coding. Here i am trying to get the values from Jtable and store it in string, to create a database table. The Whole Code is here: http://textuploader.com/?p=6&id=zVEWY
Coding :
int row = table.getRowCount();
int column = table.getColumnCount();
for (int j = 0; j < row; j++) {
for (int i = 0; i < column; i++) {
//System.out.println(table.getValueAt(j, i));
String readstr = (String) table.getValueAt(j, i); // I WANT TO ACCESS THIS STRING
}
}
try{
// FILE READ AND TABLE CREATE
String tname="example";
String dbname="DB123";
Class.forName("com.mysql.jdbc.Driver");
Connection conn= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/DB123","root","");
Statement stmt=(Statement) conn.createStatement();
System.out.println("connect");
DatabaseMetaData dmd = (DatabaseMetaData) conn.getMetaData();
while((readstr=brr.readLine())!=null) // TO USE IT HERE
{
ResultSet rs = dmd.getTables(null,"MigrationDB", "example", null);
//set not null column
if (String.valueOf(readstr).contains("NO"))
readstr=readstr.replaceAll("NO", "not null");
else if(String.valueOf(readstr).contains("YES"))
readstr=readstr.replaceAll("YES", "");
if(String.valueOf(readstr).contains("PRIMARY"))
readstr=readstr.replaceFirst("PRIMARY","primary key");
System.out.println("replace string "+readstr);
int k=1;
if (!rs.next())
{
stmt.executeUpdate("CREATE TABLE "+tname+ "("+readstr+")");
}
else{
System.out.println(readstr);
stmt.executeUpdate("ALTER TABLE "+tname+ " ADD("+readstr+")");
System.out.println("altered");
}
}
}
catch (Exception e){}
}
}
Thank You.
解决方案
You need to understand what local variables are and what is their lifetime. I suggest reading through this tutorial: http://www.tutorialspoint.com/java/java_variable_types.htm
In you case, just declaring readstr outside the for loop would do what you want.
String readstr;
for(//loop condition) {
readstr = read value ;
}
这篇关于在java中循环之外访问字符串变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文