上代码:效果是,如果testDb数据库中有表,如果有guid字段,则guid字段为varchar(500 )。 如果需要,可以继续修改包。
导入Java.SQL.*;
公共类测试qljdbc {
publicstaticvoidmain (字符串[ ] args ) {
//将数据库dbName中的所有字段fieldNameToUpdate的长度更改为500
String dbName='testDb '; //要操作的数据库名称
String fieldNameToUpdate='guid '; //要修改的字段名称
string URL=' JDBC : MySQL ://192.168.1.6433603306/testdb ';
String userName='user1';
String password='1234 ';
连接连接=null;
try {
//加载驱动器类
class.forname (com.MySQL.JDBC.driver );
}catch(exceptione ) {
e .打印堆栈跟踪(;
}
try {
con=驱动程序管理器. getconnection (URL,userName,password );
}catch(exceptione ) {
e .打印堆栈跟踪(;
}
//现在可以操作MySql数据库了
try {
statement stmt=con.create statement (;
resultset RS=stmt.execute query (show tables );
//返回查询结果
while(RS.next ) )
{
stringtablename=RS.getstring(1;
string SQL _ tab columns=' showcolumnsfrom ' tablename ' from ' dbname;
preparedstatementprestatement=con.preparestatement (SQL _ tab columns );
resultset rs2=pre statement.execute query (;
while(RS2.next ) )
{
string field name=rs2.getstring (1;
if (fieldnametoupdate.equals (field name ) ) )
//修改对应表中的字段(如果有)
updatetablefield(con,tableName,fieldName;
System.out.println (表更改字段) fieldName );
}
}
preStatement.close (;
system.out.println(tablename;
}
rs.close (;
stmt.close (;
con.close (;
}catch(exceptione ) {
e .打印堆栈跟踪(;
}
}
私有状态语音更新表字段(连接连接,字符串表名称,
字符串字段名称(throws sqlexception {
string SQL=' alter table ' tablename ' modify column ' field name ' varchar (500 );
preparedstatementprestatement=con.preparestatement (SQL;
preStatement.executeUpdate (;
preStatement.close (;
}
}