1、错误描述
java.util.MissingFormatArgumentException: Format specifier 'd'
at java.util.Formatter.format(Formatter.java:2487)
at java.util.Formatter.format(Formatter.java:2423)
at java.lang.String.format(String.java:2792)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
2、错误原因
/**
*
* 项目名称:DABF
* 项目包名:com.you.model
* 文件名称:Format.java
* 类型名称:Format
* 创建作者:游海东
* 创建日期:2017-2-17
* 创建时间:下午7:18:27
* 版权所有:
* 项目版本:V1.0
*/
package com.you.model;
/**
* 文件名称:Format.java
* 文件类型:Format
* 文件包名:com.you.model
* 创建作者:游海东
* 创建日期:2017-2-17
* 创建时间:下午7:18:27
*/
public class Format
{
/**
*
* 方法描述:拼接查询SQL语句
* 创建作者:游海东
* 创建日期:2017-2-17
* 创建时间:下午7:21:10
* 方法名称:querySql
* 方法类型:Format
* 返回类型:String
* @return
*/
public static String querySql()
{
StringBuilder sql = new StringBuilder();
sql.append("select s.stu_no,s.stu_name,s.stu_age,s.stu_sex,c.stu_score from t_stu_%d s left join t_score_%d c on s.stu_id = c.stu_id ");
return sql.toString();
}
/**
* 方法描述:
* 创建作者:游海东
* 创建日期:2017-2-17
* 创建时间:下午7:18:27
* 方法名称:main
* 方法类型:Format
* 返回类型:void
* @param args
*/
public static void main(String[] args)
{
String sql = querySql();
sql = String.format(sql, 12);
System.out.println(sql);
}
}
SQL语句中含有分表,需要根据月份取表名,这里使用%d占位;使用SQL语句时,利用String.format方法替代%d,但是这个方法不能取代两个或以上的数值类型。3、解决办法
/**
*
* 项目名称:DABF
* 项目包名:com.you.model
* 文件名称:Format.java
* 类型名称:Format
* 创建作者:游海东
* 创建日期:2017-2-17
* 创建时间:下午7:18:27
* 版权所有:
* 项目版本:V1.0
*/
package com.you.model;
/**
* 文件名称:Format.java
* 文件类型:Format
* 文件包名:com.you.model
* 创建作者:游海东
* 创建日期:2017-2-17
* 创建时间:下午7:18:27
*/
public class Format
{
/**
*
* 方法描述:拼接查询SQL语句
* 创建作者:游海东
* 创建日期:2017-2-17
* 创建时间:下午7:21:10
* 方法名称:querySql
* 方法类型:Format
* 返回类型:String
* @return
*/
public static String querySql()
{
StringBuilder sql = new StringBuilder();
sql.append("select s.stu_no,s.stu_name,s.stu_age,s.stu_sex,c.stu_score from t_stu_%s s left join t_score_%s c on s.stu_id = c.stu_id ");
return sql.toString();
}
/**
* 方法描述:
* 创建作者:游海东
* 创建日期:2017-2-17
* 创建时间:下午7:18:27
* 方法名称:main
* 方法类型:Format
* 返回类型:void
* @param args
*/
public static void main(String[] args)
{
String sql = querySql();
sql = String.format(sql, "12","12");
System.out.println(sql);
}
}
将“%d”改为“%s”,然后在String.format(sql,"12","12") 传入两个参数
运行结果为
SELECT
s.stu_no,
s.stu_name,
s.stu_age,
s.stu_sex,
c.stu_score
FROM
t_stu_12 s
LEFT JOIN t_score_12 c ON s.stu_id = c.stu_id