java对oracle进行增删改查

 导入jar包【ojdbc+数值】

在oracle安装目录下搜索ojdbc即可,随便选取其中一个即可

 

 对添加的相关代码做了一个精简

package TEST;
import java.sql.*;

public class TestConnection {
    static String url = "jdbc:oracle:thin:@localhost:1521:xxxx";
    static String user = "xxxx";
    static String password = "xxxx";
    private static Connection connection;
    private static Statement statement;
    private static PreparedStatement preparedStatement;
    public static void add(){
        try {
            //加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //获取connection
            connection = DriverManager.getConnection(url,user,password);
            //添加,方式一(存在SQL注入)
            statement = connection.createStatement();
            statement.executeUpdate("insert into test_a(id,name) values ('id53','name53')");
            //添加,方式二(防止SQL注入)
            preparedStatement = connection.prepareStatement("insert into test_a(id,name) values (?,?)");
            preparedStatement.setString(1,"id54");
            preparedStatement.setString(2,"name54");
            preparedStatement.executeUpdate();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            try {
                //关闭preparedStatement
                if(preparedStatement != null){
                    preparedStatement.close();
                }
                //关闭statement
                if (statement != null){
                    statement.close();
                }
                //关闭connection
                if (connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //测试
    public static void main(String[] args){
        add();
    }

}

对oracle数据库进行增删改查(未精简)

import java.sql.*;

public class TestConn {
    static Connection connection;
    static Statement statement;
    static ResultSet resultSet;
    static PreparedStatement preparedStatement;
    //初始化
    public static void initialize(){
        String url = "jdbc:oracle:thin:@localhost:1521:xxxx";
        String user = "xxxx";
        String password = "xxxx";
        //1.加载驱动
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        //2.获取Connection
        try {
            connection = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //添加
    public static void add(){
        initialize();
        String addSql = "insert into test_a(id,name) values('2','3')";
        try {
            statement = connection.createStatement();
            int i = statement.executeUpdate(addSql);
            if (i<1){
                System.out.println("添加失败!");
            }else{
                System.out.println("添加成功!");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            closeStatement(statement);
            closeConnection(connection);

        }
    }
    //删除和修改
    public static void delOrUpd(){
        initialize();
        //String Sql = "delete test_a where id = '2'";
        String sql = "update test_a set name = 'xxc' where id = '1'";
        try {
            statement = connection.createStatement();
            int i =  statement.executeUpdate(sql);
            if (i<1){
                System.out.println("失败!");
            }else {
                System.out.println("成功!");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            closeStatement(statement);
            closeConnection(connection);
        }
    }
    //查询(SQL注入风险)
    public static void query(){
        initialize();
        String querySql = "select ID,NAME from TEST_A t";
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(querySql);
            while (resultSet.next()){
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                System.out.println("id:"+id+" name:"+name);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            closeResultSet(resultSet);
            closeStatement(statement);
            closeConnection(connection);
        }
    }
    //查询(无SQL注入)
    public static void query2(){
        initialize();
        String query2Sql = "select ID,NAME from TEST_A t where ID = ?";
        try {
            preparedStatement = connection.prepareStatement(query2Sql);
            preparedStatement.setString(1,"id1");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                System.out.println("id:"+id+" name:"+name);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            closeResultSet(resultSet);
            closePreparedStatement(preparedStatement);
            closeConnection(connection);
        }
    }
    //关闭connection
    public static void closeConnection(Connection connection){
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //关闭statement
    public static void closeStatement(Statement statement){
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //关闭preparedStatement
    public static void closePreparedStatement(PreparedStatement preparedStatement){
        if (preparedStatement != null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //关闭ResultSet
    public static void closeResultSet(ResultSet resultSet){
        if (resultSet !=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    //执行测试
    public static void main(String[] args){
        //只测试query2
        query2();
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值