如何集成创建/接收连接,查询数据库以及可能使用Java 7的自动资源管理(try-with-resources语句)处理结果的常用JDBC习惯用法?(教程)
在Java 7之前,通常的模式是这样的:
Connection con = null;
PreparedStatement prep = null;
try{
con = getConnection();
prep = prep.prepareStatement("Update ...");
...
con.commit();
}
catch (SQLException e){
con.rollback();
throw e;
}
finally{
if (prep != null)
prep.close();
if (con != null)
con.close();
}
Run Code Online (Sandbox Code Playgroud)
使用Java 7,您可以选择:
try(Connection con = getConnection(); PreparedStatement prep = con.prepareConnection("Update ..."){
...
con.commit();
}
Run Code Online (Sandbox Code Playgroud)
这将关闭Connection和PreparedStatement,但滚动呢?我无法添加包含回滚的catch子句,因为该连接仅在try块中可用.
你还在try块之外定义连接吗?这里的最佳做法是什么,尤其是在使用连接池的情况下?
是否可以@RolesAllowed在JAX-WS Web服务上使用注释,如果可以,如何使用?
我在使用基本身份验证的glassfish 3.1.1上有一个web服务,但@RolesAllowed忽略了使用的限制.角色信息应该可用,因为我可以像这样访问它:
@Resource
WebServiceContext wsContext;
if (wsContext.isUserInRole("READ"))
log.info("Role: READ");
Run Code Online (Sandbox Code Playgroud)
我得到了预期的角色,但仍然可以访问所有方法,即使@RolesAllowed设置为不同的角色.@DenyAll工作不顺利.
如果不支持这些注释,是否可以使用部署描述符来管理基于用户角色的Web服务方法的访问?
编辑:
JAVA EE 6教程的这一部分描述了@RolesAllowed注释的用法.它读
对于Java EE组件,可以使用@DeclareRoles和@RolesAllowed元数据注释定义安全角色.
Web服务未在本教程的第一部分中列为Java EE组件,因此看起来不支持安全注释.
编辑2 继Izan的帖子后,我再试一次.这是我做的:
@Webservice
@DeclareRoles(value = {"READ", "UPDATE", "DELETE"})
public class ServiceImpl implements Service {
@Override
@WebMethod(operationName = "helloWorld")
@RolesAllowed({"NONE"})
public String helloWorld() throws Exception {
return "Hello World!";
}
}
Run Code Online (Sandbox Code Playgroud)
使用这种设置,无论设置什么角色,每个人都可以访问该方法.用户获得身份验证(可以在audit.log中看到),但不会进行授权.如上所述,我可以访问该角色WebServiceContext(我实际上使用此信息进行手动授权).
添加@Stateless注释,让我使用安全注释.所以@permitAll按预期工作.但是使用角色仍然不起作用,因为用户现在无法进行身份验证.它们显示ANONYMOUS在审核日志中,并且拒绝访问它们.
我web.xml看起来像这样:
<?xml …Run Code Online (Sandbox Code Playgroud) 我正在运行glassfish 3.1.1。在我的开发计算机(使用Windows XP)上运行了一段时间,但是今天早晨它无法启动。运行
asadmin start-domain domain1
报告:
Waiting for domain1 to start .....Error starting domain domain1.
The server exited prematurely with exit code 1.
Before it died, it produced the following output:
Launching GlassFish on Felix platform
Completed shutdown of GlassFish runtime
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.enterprise.glassfish.bootstrap.GlassFishMain.main(GlassFishMa
in.java:97)
at com.sun.enterprise.glassfish.bootstrap.ASMain.main(ASMain.java:55)
Caused by: java.lang.NullPointerException
at com.sun.enterprise.server.logging.GFFileHandler.postConstruct(GFFileH
andler.java:159)
at com.sun.hk2.component.AbstractCreatorImpl.inject(AbstractCreatorImpl.
java:131)
at com.sun.hk2.component.ConstructorCreator.initialize(ConstructorCreato
r.java:91)
at com.sun.hk2.component.AbstractCreatorImpl.get(AbstractCreatorImpl.jav
a:82)
at com.sun.hk2.component.SingletonInhabitant.get(SingletonInhabitant.jav …Run Code Online (Sandbox Code Playgroud) 我有一个名为 Account 的强类型数据表,我在 Account.FullName 上排序:
DataView dvAccount = new DataView(dtAccount)
dvAccount.Sort = "FullName desc";
Run Code Online (Sandbox Code Playgroud)
全名是查询后从我的数据集中生成的字段,基于名字、中间名、姓氏等。不幸的是,这意味着按 SQL 查询排序不是一个选项。
首先我尝试得到这样的表:
dtAccount = dvAccount.Table()
Run Code Online (Sandbox Code Playgroud)
但这给了我 Dataview 所基于的原始表。所以在网上阅读后我发现我应该使用该DataView.ToTable()函数而不是该DataView.Table()函数:
dtAccount = dvAccount.ToTable() as dsAccount.AccountDataTable; // returns null
dtAccount = ((dsAccount.AccountDataTable) dvAccount.ToTable()); // gives Convertion to Typed Datatable Error
Run Code Online (Sandbox Code Playgroud)
现在我遇到的问题是我的帐户表是强类型的。因此,在线搜索告诉我,我可以使用DataTable.Merge()函数或DataTable.ImportRow()每一行,但这些被告知是一个非常繁重的过程,因为每一行都会检查类型。这种情况的最佳实践解决方案是什么?