0 0

Oracle JDBC 没响应,是不是BUG?5

在Java 中执行下面SQL语句没有响应,DEBUG 无法跟踪代码,但此语句在SQL DEVELOPER 执行没问题,在Java代码中执行其他 SQL 语句基本也没问题。


insert into T1 (ID, VALUE) select ID, VALUE from T2



import 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);
	}
}
2013年8月08日 18:36

5个答案 按时间排序 按投票排序

0 0

采纳的答案

如果数据库连接没有报错的话,试试先关闭取消自动提交,

conn.setAutoCommit(false) ; 
,在最后手动提交,
conn.commit();

2013年8月09日 08:52
0 0

Class.forName() 没有

2013年8月09日 13:49
0 0

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();
		}
	}

2013年8月09日 10:23
0 0

http://wyf289283641.iteye.com/blog/1911274

2013年8月09日 09:48
0 0

问题可能出在Connection没有提交到数据库,而且你DriverManager 也没有初始化啊~
一般用Class.formane("oracle.jdbc.driver.OracleDriver")

2013年8月08日 20:59

相关推荐

Global site tag (gtag.js) - Google Analytics