如何使用java对oracle数据库进行增删查改

你好我们是的Java课程是用PL/SQL和Oracle数据库就是用eclipse经由过程PL/SQL操作Oracle数据库的你可以去懂得一下... 你好我们是的Java课程是用PL/SQL和Oracle数据库就是用eclipse经由过程PL/SQL操作Oracle数据库的你可以去懂得一下 展开
 我来答
匿名用户
2013-11-02
展开全部
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BaseDao {
// private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
// private static final String URL="jdbc:sqlserver://localhost:1433;databaseName=orcl";
public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
private static final String USERNAME="sa";
private static final String PASSWORD="sa";
Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;

public Connection getConnection(){
try {
Class.forName(DRIVER);
try {
conn=DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

return conn;
}
public void close(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void setParams(Object...params){
if(null!=params){
for(int i=0;i<params.length;i++){
try {
pst.setObject(i+1, params[i]);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public int executeUpdate(String sql,Object...params){
int result=-1;
conn=getConnection();
try {
pst=conn.prepareStatement(sql);
setParams(params);
result=pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public List<Map<String, Object>> executeQuery(String sql, Object... params) {
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>(); conn = getConnection();
try {
pst = conn.prepareStatement(sql);
setParams(params);
rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Map<String, Object> row = null;
while (rs.next()) {
row = new HashMap<String, Object>();
for (int i = 0; i < columnCount; i++) {
String columnName = rsmd.getColumnName(i + 1);
Object value = rs.getObject(columnName);
row.put(columnName.toLowerCase(), value);
}
result.add(row);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return result;
}
public int execute(String sql, Object... params) {
int result = -1;
conn = getConnection();
try {
pst = conn.prepareStatement(sql);
setParams(pst, params); result = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return result;
} public ResultSet query(String sql, Object... params) {
conn = getConnection();
try {
pst = conn.prepareStatement(sql);
setParams(pst, params);
rs = pst.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式