如何在java中获取Oracle里获取自增ID 在线等
展开全部
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)
{
String err = "连接数据库时出错:创建不了数据库连接";
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里调用这个javaBeanjsp就是一个servlet操作数据库没什么区别
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)
{
String err = "连接数据库时出错:创建不了数据库连接";
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里调用这个javaBeanjsp就是一个servlet操作数据库没什么区别
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |