在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);
}}
4 回答
湖上湖
TA贡献2003条经验 获得超2个赞
如果数据库连接没有报错的话,试试先关闭取消自动提交,[code="java"]conn.setAutoCommit(false) ; [/code],在最后手动提交,conn.commit();
慕斯709654
TA贡献1840条经验 获得超5个赞
问题可能出在Connection没有提交到数据库,而且你DriverManager 也没有初始化啊~
一般用Class.formane("oracle.jdbc.driver.OracleDriver")
慕的地8271018
TA贡献1796条经验 获得超4个赞
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(); } }
添加回答
举报
0/150
提交
取消