myeclipse连接oracle数据库后增删改查方法怎么写,我只写出了查询!
packageorg.accp.myservlet.entity;importjava.sql.*;publicclassBaseDao{//dbUrl数据库连接串信息,...
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) {
rs = null;
try {
rs = conn.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//执行修改
public ResultSet update(String sql){
return rs;
}
//执行添加
//执行删除
public void close() {
try {
conn.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
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();
}
} 展开
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) {
rs = null;
try {
rs = conn.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
//执行修改
public ResultSet update(String sql){
return rs;
}
//执行添加
//执行删除
public void close() {
try {
conn.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
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();
}
} 展开
4个回答
展开全部
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();
}
}
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();
}
}
展开全部
你可以将得到连接的代码进行封装,让其返回一个可用的连接,然后就是写几个增删改查的方法,每个方法中去调用工具类得到连接,后面的就是和你写的一样了。查询用executeQuery,增删改用executeUpdate
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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返回集合然后遍历,增删改没这一步
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返回集合然后遍历,增删改没这一步
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
只需要更改SQL语句即可,其他改动的相当的少。
追问
我试过,但是方法写的不对!! 能帮忙写下后面三个方法么?
追答
其他三种操作不能调用 conn.executeQuery(sql); 这个法的,得调用conn的其他方法,貌似叫什么executeupdate还是什么的,你自己在conn后面打个点号然后看一下具体是什么方法名吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询