get - Access string variable outside for loop in java? -
hi folks want access string variable outside loop, can use further coding. here trying values jtable , store in string, create database table. whole code here: http://textuploader.com/?p=6&id=zvewy
coding : int row = table.getrowcount(); int column = table.getcolumncount(); (int j = 0; j < row; j++) { (int = 0; < column; i++) { //system.out.println(table.getvalueat(j, i)); string readstr = (string) table.getvalueat(j, i); // want access string } } try{ // file read , 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) // use 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 understand local variables , lifetime. suggest reading through tutorial: http://www.tutorialspoint.com/java/java_variable_types.htm
in case, declaring readstr outside loop want.
string readstr; for(//loop condition) { readstr = read value ; }
Well explained . There are good example regarding java variable Java Variable Examles
ReplyDelete