myeclipse连接oracle数据库后增删改查方法怎么写,我只写出了查询!

2024-12-05 05:16:45
推荐回答(4个)
回答1:

package org.accp.myservlet.entity;
import java.sql.*;
public class BaseDao {
//dbUrl数据库连接串信息,其中“1521”为端口,“ora9”为sid
String dbUrl = "jdbc:oracle:thin:@localhost:1521:oracle";
//theUser为数据库用户名
String theUser = "root";
//thePw为数据库密码
String thePw = "root";
//几个数据库变量
Connection c = null;
Statement conn;
ResultSet rs = null;
//初始化连接
public BaseDao() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//与url指定的数据源建立连接
c = DriverManager.getConnection(dbUrl, theUser, thePw);
//采用Statement进行查询
conn = c.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
//执行查询
public ResultSet executeQuery(String sql) throws SQLException {
rs = null;
try {
rs = conn.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
} finally{
close();

}
return rs;
}
//执行修改
public void update(String sql) throws SQLException{
int len = 0;
try {
len = conn.executeUpdate(sql);
if(len>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}

}

//执行添加
public void Add(String sql) throws SQLException{
boolean bool = false;
try {
bool = conn.execute(sql);
if(bool){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}

}

//执行删除
public void Delet(String sql) throws SQLException{
boolean bool = false;
try {
bool = conn.execute(sql);
if(bool){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
close();
}

}

public void close() {
try {
conn.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
ResultSet newrs;
BaseDao newjdbc = new BaseDao();
newrs = newjdbc.executeQuery("select * from users");
try {
while (newrs.next()) {
System.out.print(newrs.getString("USERID"));
System.out.print(":"+newrs.getString("USERNAME"));
System.out.print(":"+newrs.getString("PASSWB"));
System.out.print(":"+newrs.getString("EMAIL"));
System.out.println(":"+newrs.getString("GRADE"));
}
} catch (Exception e) {
e.printStackTrace();
}
newjdbc.close();
}
}

回答2:

你可以将得到连接的代码进行封装,让其返回一个可用的连接,然后就是写几个增删改查的方法,每个方法中去调用工具类得到连接,后面的就是和你写的一样了。查询用executeQuery,增删改用executeUpdate

回答3:

public boolean delete(int id) {
Connection con=null;
PreparedStatement pst=null;
try{
con=CF.getConnection();
String sql="delete from user where id=?";
pst=(PreparedStatement) con.prepareStatement(sql);
pst.setInt(1,id);
pst.executeUpdate();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
CF.close(null,pst,con);
}
}
这是根据id删除
public boolean add(User user) {
Connection con=null;
PreparedStatement pst=null;
try{
con=CF.getConnection();
String sql="insert into user(username,password) values (?,?)";
pst=(PreparedStatement) con.prepareStatement(sql);
pst.setString(1,user.getUsername());
pst.setString(2,user.getPassword());
pst.executeUpdate();
System.out.println(user.getUsername()+"~~"+user.getPassword());
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
CF.close(null,pst,con);
}
}
这是添加
修改跟添加基本一样.只是sql语句不同
查询就是使用ResultSet返回集合然后遍历,增删改没这一步

回答4:

只需要更改SQL语句即可,其他改动的相当的少。