问题描述
在Java 中执行下面SQL语句没有响应,DEBUG 无法跟踪代码,但此语句在SQL DEVELOPER 执行没问题,在Java代码中执行其他 SQL 语句基本也没问题。insert into T1 (ID, VALUE) select ID, VALUE from T2import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import oracle.jdbc.driver.OracleDriver;public class TestOracleUtil {public static void main(String[] args) throws SQLException {String sql = "insert into T1 (ID, VALUE) select ID, VALUE from T2";update(getConnection(), sql);}public static Connection getConnection() {new OracleDriver();String serverName = "192.168.1.3";String portNumber = "1521";String sid = "ORCL";String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":"+ sid;String username = "test";String password = "test";try {Connection conn = DriverManager.getConnection(url, username,password);return conn;} catch (SQLException e) {e.printStackTrace();return null;}}static void update(Connection conn, String sql) throws SQLException{Statement smt = conn.createStatement();smt.executeUpdate(sql);}}
解决方案
如果数据库连接没有报错的话,试试先关闭取消自动提交,conn.setAutoCommit(false) ; ,在最后手动提交,conn.commit();
解决方案二:
Class.forName() 没有
解决方案三:
try { Class.forName(sqldriver); c = DriverManager.getConnection(connURL);return c;} catch (Exception e) {e.printStackTrace();if (c != null)try {c.close();} catch (SQLException e1) {}}return c;}public void closeConnection(Connection c, Statement s, ResultSet r) {try {if (r != null)r.close();} catch (Exception e) {e.printStackTrace();}try {if (s != null)s.close();} catch (Exception e) {e.printStackTrace();}try {if (c != null)c.close();} catch (Exception e) {e.printStackTrace();}}
解决方案四:
http://wyf289283641.iteye.com/blog/1911274
解决方案五:
问题可能出在Connection没有提交到数据库,而且你DriverManager 也没有初始化啊~ 一般用Class.formane("oracle.jdbc.driver.OracleDriver")