高分求救:麻烦帮我实现一下,我不会用JNDI实现,多谢!

MichaelXiang 2004-01-27 04:56:26
class test{
public test(){
try{
取得初始化的Context;
用lookup()取得DB的Datasource(JNDI Name is testDB)
}catch{
}
}

public String GetAField(char userID)
{
try{
从Datasource取得连接
取得Statement
取得ATAB的FieldOne字段(WHERE id=USERID)
SQL语句(向Atab中添加一条数据)
执行SQL语句
}catch{
if(SQL语句执行失败){
System.out.println("SQL语句执行失败.");
}
return FieldOne字段
}
}


用Tomcat,请问
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,icf);
properties.put(Context.PROVIDER_URL, url);
……
中的icf和url怎么写?icf对应的包在哪里可以下载?谢谢!
...全文
72 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
MichaelXiang 2004-01-28
  • 打赏
  • 举报
回复
我没有用过JNDI,能不能发个例子给我呀?xiangsj@mail.bcl.com.cn
Tomcat + Oracle + Linux 用JNDI的,谢谢!
augustbu 2004-01-27
  • 打赏
  • 举报
回复
public String GetAField(char userID)
{
try {
// get the connection
connection = dataSource.getConnection();

// get the first free id
PreparedStatement stmt = connection.prepareStatement( selectStatement );
ResultSet resultSet = stmt.executeQuery();
resultSet.next();
firstIdOfBlock = resultSet.getLong( 1 );
stmt.close();

// add blockSize to the first free id
stmt = connection.prepareStatement( updateStatement );
stmt.setLong( 1, ( firstIdOfBlock + blockSize ) );
int nbrOfUpdatedRecords = stmt.executeUpdate();
stmt.close();

// there may only be one record in the table
if ( nbrOfUpdatedRecords != 1 ) {
throw new RuntimeException( "more then one record was updated by statement '" + updateStatement + "'" );
}

} catch (SQLException e) {
context.setRollbackOnly();
if ( e.getMessage().indexOf( "Table not found" ) != -1 ) {
throw new SQLException( "Did you forget to create the database ? : SequenceSession-EJB couldn't create new block of id's : " + e.getMessage() );
} else {
throw new SQLException( "SequenceSession-EJB couldn't create new block of id's : " + e.getMessage() );
}
} catch (Throwable t) {
context.setRollbackOnly();
t.printStackTrace();
throw new RuntimeException( "SequenceSession-EJB couldn't create new block of id's : " + t.getMessage() );
} finally {
if ( connection != null ) {
try {
connection.close();
} catch (Throwable t ) {
t.printStackTrace();
System.out.println( "SequenceSession-EJB couldn't close connection while fetching a next block of database id's : " + t.getMessage() );
}
}
}

}
augustbu 2004-01-27
  • 打赏
  • 举报
回复
关于icf和url,知道weblogic和jboss,websphere的,tomcat的naming service没有研究过
augustbu 2004-01-27
  • 打赏
  • 举报
回复
public test(){
try {
// get the DataSource from JNDI
InitialContext initialContext = new InitialContext();
Object object = initialContext.lookup( "testDB");
dataSource = (DataSource) PortableRemoteObject.narrow( object, DataSource.class );
} catch( Throwable t ) {
t.printStackTrace();
throw new RuntimeException( "couldn't find DataSource '" + dataSourceName + "' in JNDI-registry : " + t.getMessage() );
}
}

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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