当我在Scala中运行删除查询时,什么都没有发生

问题描述 投票:0回答:1

我是Java / scala的初学者,我尝试对Oracle数据库执行一些查询。我的SELECT正常运行,但是当我尝试简单的删除时,什么也没发生,因此我不得不在intellij终端中止执行。

这里是我的代码:

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet, SQLException, Statement}


object ScalaJdbcConnectSelect extends App {

    // jdbc driver name and database URL
    val JDBC_DRIVER = "oracle.jdbc.OracleDriver";
    val DB_URL      = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XXX)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=XXX)(PORT=1521))(FAILOVER=on)(LOAD_BALANCE=on))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XXX)))";

    // database credentials
    val USER = "sys as sysdba";
    val PASS = "XXX";

    var conn: Connection = _;
    var stmt: Statement = _;


    def insertQuery(query : String): Unit = {
      try {
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        stmt = conn.createStatement;

        // create the statement, and run the select query
        val statement = conn.createStatement();
        statement.executeUpdate(query);
        conn.commit();

        // cleanup
        stmt.close();
        conn.close();
      } catch {
        case e: Throwable => e.printStackTrace;
      } finally {
        conn.close();
      }
    }

    def executeQuery(query : String): Unit = {
      try {
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        stmt = conn.createStatement;
        val rs: ResultSet = stmt.executeQuery(query);
        /*while (rs.next()) {
          println(rs.getString(1));
        }*/

        // cleanup
        stmt.close();
        conn.close();
      } catch {
        case e: Throwable => e.printStackTrace;
      } finally {
        conn.close();
      }
    }

  val sql = "DELETE FROM APPIVB.TDEMANDESOUSCRIPTION WHERE ID in ('4001111001')";
  //val sql = "SELECT * FROM APPIVB.TDEMANDESOUSCRIPTION WHERE ID in ('4001111001')";

  executeQuery(sql);
  println("Record deleted successfully");
}

您对我的问题有想法吗?谢谢

java jdbc
1个回答
2
投票

对于删除查询,您将必须使用-

stmt.executeUpdate(query);

enter image description here

https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

© www.soinside.com 2019 - 2024. All rights reserved.