args 怎么放合适的值,而且程序也无法运行,这是thinking in java中第627页例子

weixin_39872541 2018-04-04 09:56:47
package chapter20.second.database;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class TableCreator {
public static void mian(String[] args) throws Exception {
System.out.println(args.length);
if (args.length < 1) {
System.out.println("argument:annotated classes");
System.exit(0);
}
for (String className : args) {
Class<?> cl = Class.forName(className);
System.out.println(cl.getName());
DBTable dbTable = cl.getAnnotation(DBTable.class);
if(dbTable == null) {
System.out.println("NO DBTable in annotations in class" + className);
continue;
}
String tableName = dbTable.name();
if (tableName.length() < 1) {
tableName = cl.getName().toUpperCase();
}
List<String> columnDefs = new ArrayList<String>();
for (Field field : cl.getDeclaredFields()) {
String columnName = null;
Annotation[] anns = field.getDeclaredAnnotations();
if (anns.length < 1) {
continue;
}
if (anns[0] instanceof SQLInteger) {
SQLInteger sInt = (SQLInteger) anns[0];

if (sInt.name().length() < 1) {
columnName = field.getName().toUpperCase();
}else {
columnName = sInt.name();
}
columnDefs.add(columnName + " INT " + getConstraints(sInt.constraints()));
}

if (anns[0] instanceof SQLString) {
SQLString sString = (SQLString) anns[0];
if (sString.name().length() < 1 ) {
columnName = field.getName().toUpperCase();
}else {
columnName = sString.name();
}
columnDefs.add(columnName + "VARCHAR("+sString.value()+")" + getConstraints(sString.constraints()));
}

StringBuilder createCommand = new StringBuilder("CREATE TABLE" + tableName +"(");
for (String columnDef : columnDefs) {
createCommand.append("\n " + columnDefs + ",");
String tableCreate = createCommand.substring(0,createCommand.length() - 1) + ");";
System.out.println("Table Creation SQL for " +className + "is:\n" + tableCreate);
}
}
}
}
private static String getConstraints(Constraints con) {
String constraints = "";
if (!con.allowNull()) {
constraints += "NOT NULL";
}
if (!con.primaryKey()) {
constraints += "PRIMARY KRY";
}

if (!con.unique()) {
constraints += "UNIQUE";
}
return constraints;
}
}
...全文
379 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,635

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧