如何用java取得在oracle後台取得的sequence的序號

[复制链接]
查看11 | 回复5 | 2011-11-1 16:19:41 | 显示全部楼层 |阅读模式
各位大俠
前台是jsp /java時如何將由在oracle 後台取得的
selectseq1.nextval from dual 取得的值在jsp中應用,如何返回前台,
謝謝各位。
回复

使用道具 举报

千问 | 2011-11-1 16:19:41 | 显示全部楼层
直接给前台赋值即可
回复

使用道具 举报

千问 | 2011-11-1 16:19:41 | 显示全部楼层
与你平常使用JDBC一样,你要是喜欢用列名,可以这样
select seq.nextvalcolname from dual
回复

使用道具 举报

千问 | 2011-11-1 16:19:41 | 显示全部楼层
各位大哥,我是想知道如何將這個seq.nextval的值賦給jsp中的變量
回复

使用道具 举报

千问 | 2011-11-1 16:19:41 | 显示全部楼层
public class SeqManager {
protected String dataSource = "java:/OracleDS";
public SeqManager() {
}
/**
* 从数据库连接中取的一连接
*
* @param String dataSource 为数据源JNDI
*
*
* 返回值为数据库连接
*
*/
protected Connection getConnection(String dataSource)
{
Connection con = null;
try
{
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dataSource);
con = ds.getConnection();
System.out.println("connection is :" + con);
// con.setAutoCommit(false);
} catch(NamingException e)
{
String err = "连接数据库时出错:找不到JNDI名称" + dataSource;
System.out.println(e);
e.printStackTrace();
} catch (SQLException ex)
{
Stringerr = "连接数据库时出错:创建不了数据库连接";
System.out.println(ex);
ex.printStackTrace();
}
return con;
}
/**
* 取Sequence的公共方法
*
* @param String sql 为传进的SQL语句
*
*
* 返回值为Sequence值
*
*/
protected Long getNextValOfSeq(String sql) throws SQLException
{
Connection con = getConnection(dataSource);
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
try
{
if (rs.next()) {
return new Long(rs.getLong(1));
}
else {
return null;
}
}catch(SQLException e)
{
throw e;
}finally
{
rs.close();
pst.close();
con.close();
}
}
public Long getNextValOfStudEntrScore() throws SQLException
{
String sql = " Select SeqStudEntrScore.NextVal From Dual ";
try
{
return getNextValOfSeq(sql);
}catch (SQLException e)
{
String errorMessage = "取***Sequence时出错.";
e.printStackTrace();
throw new SQLException(errorMessage);
}
}
}
在jsp里调用这个javaBean
jsp就是一个servlet
操作数据库没什么区别
回复

使用道具 举报

千问 | 2011-11-1 16:19:41 | 显示全部楼层
謝謝指點
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行