日期:2014-05-17  浏览次数:20643 次

请问如何在java中实现对oracle数据库的操作
先谢谢大家了,我这个代码只能实现查询,请问应该修改才能实现在oracle中建表,增加,删除,更新操作,谢谢。
Java code
import java.sql.*;

public class ConnectOracle {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement prstm = null;
        ResultSet rs = null;
        try {
//------------------------------------------
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@127.0.0.1:1521:xu"; // 数据库连接字符串
            String user = "scott"; 
            String password = "tiger"; 
            conn = DriverManager.getConnection(url, user, password); // 获取连接
//-------------------------------------------
            prstm = conn.prepareStatement("SELECT * FROM BUDGET_BASE_DATA");
            rs = prstm.executeQuery();
            System.out.println("查询结果为:" );
            while (rs.next()) {
                System.out.println(rs.getString("ID") +"  "+ rs.getString("TYPE"));
            }
            prstm = conn.prepareStatement("create table zltest(aa number,bb varchar2(10))");
            System.out.print("done");            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 使用完毕进行相关内容的关闭,注意相关顺序
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (prstm != null) {
                try {
                    prstm.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}


------解决方案--------------------
prstm = conn.prepareStatement("insert into t(a,b,c) values(a,b,c)");
pstmt.executeUpdate();