我有一个DAO Layer可以像这样插入到DERBY db中,
try {
long id = dbAccess.insert(connection,
"INSERT INTO BOOKS(NAME, AUTHORS, PUBLISHYEAR, AVAIL) VALUES (?, ?, ?, ?)",
new ScalarHandler<BigDecimal>(),
book.getName(),
book.getAuthors(),
book.getPublishedYear(),
book.isAvailable()
).longValue();
return id;
} catch (Exception e) {
e.printStackTrace();
}
return -1L;
如果我使用ORACLE DB作为数据源,如何翻译?
答案 0 :(得分:2)
我对您的数据库一无所知,但您需要定义插入表ID的方法,并且需要为您的oracle数据库创建连接类
此外,您需要管理您的表ID增量您应该创建一个触发器,以在每次插入操作后增加表ID 我将发布一个从表中选择max id的方法,你可以在下面的链接中找到oracle触发器来增加序列
How to create id with AUTO_INCREMENT on Oracle?
首先我们需要创建从表中返回最大ID的方法
public static int getMaxBookID(Connection connection){
int id=0;
String sql = "SELECT NVL(MAX(ID),0)+1 FROM BOOK ";
try{
PreparedStatement statement = connection.prepareStatement(sql);
if(statement!=null){
try{
ResultSet results = statement.executeQuery();
if(results != null){
try{
if(results.next()){
id = results.getInt(1);
}
}
catch(Exception resultSetException) {resultSetException.printStackTrace();
}
results.close();
}
}
catch(Exception statmentExcption){statmentExcption.printStackTrace();
}
statement.close();
}
} catch (Exception generalException){generalException.printStackTrace();
}
return id;
}
这两种方法用于打开和关闭你的连接
private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_CONNECTION = "jdbc:oracle:thin:@//host:1526/databasename";
private static final String DB_USER = "username";
private static final String DB_PASSWORD = "passowrd";
public static Connection lockConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
return DriverManager.getConnection(
DB_CONNECTION, DB_USER, DB_PASSWORD);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
public static void closeMyConnection(Connection connection) {
try {
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
现在您可以将信息插入表格
public int AddBook(String name,String auth,String year , String avail){
int id=0;
Connection connection = lockConnection();
boolean ok = false;
String sql = "INSERT INTO BOOKS(ID,NAME, AUTHORS, PUBLISHYEAR, AVAIL)"
+ " VALUES(?,?,?,?,?)";
try{
PreparedStatement statement = connection.prepareStatement(sql);
if(statement!=null){
statement.setInt(1,getMaxBookID(connection));
statement.setString(2,name);
statement.setString(3,auther);
statement.setString(4,year);
statement.setString(5,avail);
try{
int count = statement.executeUpdate();
ok = count == 1;
if(!ok)id=0;
}
catch(Exception statmentExcption){statmentExcption.printStackTrace();statmentExcption.printStackTrace(); return 0 ;
}
statement.close();
}
} catch (Exception generalException){generalException.printStackTrace(); generalException.printStackTrace(); return 0;
}
closeMyConnection(connection);
return id;
}
答案 1 :(得分:1)
由于Oracle不支持IDENTITY列,因此您必须做两件事才能使其工作:
这意味着在构建prepareStatement(String sql, String[] columnNames)
时使用prepareStatement(String sql, int autoGeneratedKeys)
版本,而不是PreparedStatement
版本,因为Oracle不知道哪个列是“自动生成的”。
使用名称版本适用于所有DBMS供应商,因此使用它可以使您的代码供应商不可知:
AUTO_INCREMENT
IDENTITY
SERIAL
GENERATED ALWAYS AS IDENTITY