以下是我写的代码
public class MyJdbc {
private String host;      //本地主机
private String port;      //端口
private String sid;       //主机名
private String user;      //用户名
private String password;  //密码
private StringBuffer url = new StringBuffer("jdbc:oracle:thin:@");  //连接字符串
private Connection con;
MyJdbc(String host,String port,String sid,String user,String password){
this.host = host;
this.port = port;
this.sid = sid;
this.user = user;
this.password = password;
this.url.append(this.host).append(":").append(this.port).append(":").append(this.sid);
//System.out.println(url.toString());
}
void connect() //注册驱动和连接数据库
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("注册成功");
} catch (ClassNotFoundException e) {
System.out.println("注册失败");
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url.toString(),user,password);
System.out.println("连接成功");
} catch (SQLException e) {
System.out.println("连接失败");
e.printStackTrace();
}
}
void showTbale(String sql) //显示表的信息
{
Statement st = null;
ResultSet rs = null;
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = st.executeQuery(sql);
ResultSetMetaData rsm = rs.getMetaData();
int i = rsm.getColumnCount();
while(rs.next()){
for(int j = 1 ; j <= i ; j++){
System.out.print(rs.getString(j)+" ");
}
System.out.println("");
};
rs.close();
st.close();
} catch (SQLException e) {
System.out.println("表视图不存在");
e.printStackTrace();
}
finally{

}
}
void operateTable(String sql) //对表的操作例如 建表,插入,删除,更新。
{
Statement st = null;
try {
st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
st.executeUpdate(sql);
System.out.println("操作成功"); 
st.close();
} catch (SQLException e) {
System.out.println("表或视图不存在或无效SQL语句");
}
}
final void close(){
try {
con.close();
System.out.println("已关闭连接");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
   
   为什么我这只有插入信息的时候才会成功~其他例如操作删除,更新没用~好象显示也不能立即更新~哪个帮忙解决下~~~~

解决方案 »

  1.   

    把 ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE去掉,操作oracle多余写这参数。
      

  2.   

    你 int temp = st.executeUpdate(sql);看看 temp 返回的是什么。下面是jdk1.5 api的说明:executeUpdate
    int executeUpdate(String sql)
                      throws SQLExceptionExecutes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. Parameters:
    sql - an SQL INSERT, UPDATE or DELETE statement or an SQL statement that returns nothing 
    Returns:
    either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing 
    Throws: 
    SQLException - if a database access error occurs or the given SQL statement produces a ResultSet object