赞
踩
1最基本的操作读取
- import java.sql.*;
-
- public class TestJDBC {
-
- public static void main(String[] args) throws Exception{
- ResultSet rs = null;
- Statement stmt = null;
- Connection conn = null;
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select * from dept");
- while(rs.next()) {
- System.out.println(rs.getString("deptno"));
- System.out.println(rs.getInt("deptno"));
- }
- rs.close();
- stmt.close();
- conn.close();
- }
- }
- import java.sql.*;
- public class TestPrepStmt {
- public static void main(String[] args) throws Exception{
- PreparedStatement pstmt = null;
- Connection conn = null;
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
- pstmt = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
- pstmt.setInt(1, 33);
- pstmt.setString(2, "name");
- pstmt.setString(3, "loc");
- pstmt.executeUpdate();
- pstmt.close();
- conn.close();
- }
}
- import java.sql.*;
- public class TestProc {
- public static void main(String[] args) throws Exception {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
- CallableStatement cstmt = conn.prepareCall("{call p(?, ?, ?, ?)}");
- cstmt.registerOutParameter(3, Types.INTEGER);
- cstmt.registerOutParameter(4, Types.INTEGER);
- cstmt.setInt(1, 3);
- cstmt.setInt(2, 4);
- cstmt.setInt(4, 5);
- cstmt.execute();
- System.out.println(cstmt.getInt(3));
- System.out.println(cstmt.getInt(4));
- cstmt.close();
- conn.close();
- }
- }
- import java.sql.*;
- public class TestBatch {
-
-
- public static void main(String[] args) throws Exception {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
- /*
- Statement stmt = conn.createStatement();
- stmt.addBatch("insert into dept2 values (51, '500', 'haha')");
- stmt.addBatch("insert into dept2 values (52, '500', 'haha')");
- stmt.addBatch("insert into dept2 values (53, '500', 'haha')");
- stmt.executeBatch();
- stmt.close();
- */
-
- PreparedStatement ps = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
- ps.setInt(1, 61);
- ps.setString(2, "haha");
- ps.setString(3, "bj");
- ps.addBatch();
-
- ps.setInt(1, 62);
- ps.setString(2, "haha");
- ps.setString(3, "bj");
- ps.addBatch();
-
- ps.setInt(1, 63);
- ps.setString(2, "haha");
- ps.setString(3, "bj");
- ps.addBatch();
-
- ps.executeBatch();
- ps.close();
-
- conn.close();
-
- }
-
- }
- import java.sql.*;
- public class TestTransaction {
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:SXT", "scott", "tiger");
- conn.setAutoCommit(false);
- stmt = conn.createStatement();
- stmt.addBatch("insert into dept2 values (51, '500', 'haha')");
- stmt.addBatch("insert into dept2 values (52, '500', 'haha')");
- stmt.addBatch("insert into dept2 values (53, '500', 'haha')");
- stmt.executeBatch();
- conn.commit();
- conn.setAutoCommit(true);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch(SQLException e) {
- e.printStackTrace();
- try {
- if(conn != null)
- {
- conn.rollback();
- conn.setAutoCommit(true);
- }
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }finally {
- try {
- if(stmt != null)
- stmt.close();
- if(conn != null)
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- import java.sql.*;
-
- public class TestScroll {
- public static void main(String args[]) {
-
- try {
- new oracle.jdbc.driver.OracleDriver();
- String url = "jdbc:oracle:thin:@192.168.0.1:1521:SXT";
- Connection conn = DriverManager
- .getConnection(url, "scott", "tiger");
- Statement stmt = conn.createStatement(
- ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- ResultSet rs = stmt
- .executeQuery("select * from emp order by sal");
- rs.next();
- System.out.println(rs.getInt(1));
- rs.last();
- System.out.println(rs.getString(1));
- System.out.println(rs.isLast());
- System.out.println(rs.isAfterLast());
- System.out.println(rs.getRow());
- rs.previous();
- System.out.println(rs.getString(1));
- rs.absolute(6);
- System.out.println(rs.getString(1));
- rs.close();
- stmt.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- import java.sql.*;
- public class TestUpdataRs {
- public static void main(String args[]){
-
- try{
- new oracle.jdbc.driver.OracleDriver();
- String url="jdbc:oracle:thin:@192.168.0.1:1521:SXT";
- Connection conn=DriverManager.getConnection(url,"scott","tiger");
- Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
-
- ResultSet rs=stmt.executeQuery("select * from emp2");
-
- rs.next();
- //更新一行数据
- rs.updateString("ename","AAAA");
- rs.updateRow();
-
- //插入新行
- rs.moveToInsertRow();
- rs.updateInt(1, 9999);
- rs.updateString("ename","AAAA");
- rs.updateInt("mgr", 7839);
- rs.updateDouble("sal", 99.99);
- rs.insertRow();
- //将光标移动到新建的行
- rs.moveToCurrentRow();
-
- //删除行
- rs.absolute(5);
- rs.deleteRow();
-
- //取消更新
- //rs.cancelRowUpdates();
-
- }catch(SQLException e){
- e.printStackTrace();
- }
- }
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。