当前位置:   article > 正文

JDBC操作数据库的几种方式。_jdbc不能操作的数据库有哪些

jdbc不能操作的数据库有哪些

1最基本的操作读取

  1. import java.sql.*;
  2. public class TestJDBC {
  3. public static void main(String[] args) throws Exception{
  4. ResultSet rs = null;
  5. Statement stmt = null;
  6. Connection conn = null;
  7. Class.forName("oracle.jdbc.driver.OracleDriver");
  8. conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
  9. stmt = conn.createStatement();
  10. rs = stmt.executeQuery("select * from dept");
  11. while(rs.next()) {
  12. System.out.println(rs.getString("deptno"));
  13. System.out.println(rs.getInt("deptno"));
  14. }
  15. rs.close();
  16. stmt.close();
  17. conn.close();
  1. }
  2. }

2PreparedStatement读取

  1. import java.sql.*;
  2. public class TestPrepStmt {
  3. public static void main(String[] args) throws Exception{
  4. PreparedStatement pstmt = null;
  5. Connection conn = null;
  6. Class.forName("oracle.jdbc.driver.OracleDriver");
  7. conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
  8. pstmt = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
  9. pstmt.setInt(1, 33);
  10. pstmt.setString(2, "name");
  11. pstmt.setString(3, "loc");
  12. pstmt.executeUpdate();
  13. pstmt.close();
  14. conn.close();
  15. }
}

3调用存储过程

  1. import java.sql.*;
  2. public class TestProc {
  3. public static void main(String[] args) throws Exception {
  4. Class.forName("oracle.jdbc.driver.OracleDriver");
  5. Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
  6. CallableStatement cstmt = conn.prepareCall("{call p(?, ?, ?, ?)}");
  7. cstmt.registerOutParameter(3, Types.INTEGER);
  8. cstmt.registerOutParameter(4, Types.INTEGER);
  9. cstmt.setInt(1, 3);
  10. cstmt.setInt(2, 4);
  11. cstmt.setInt(4, 5);
  12. cstmt.execute();
  13. System.out.println(cstmt.getInt(3));
  14. System.out.println(cstmt.getInt(4));
  15. cstmt.close();
  16. conn.close();
  17. }
  18. }

4执行批处理

  1. import java.sql.*;
  2. public class TestBatch {
  3. public static void main(String[] args) throws Exception {
  4. Class.forName("oracle.jdbc.driver.OracleDriver");
  5. Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
  6. /*
  7. Statement stmt = conn.createStatement();
  8. stmt.addBatch("insert into dept2 values (51, '500', 'haha')");
  9. stmt.addBatch("insert into dept2 values (52, '500', 'haha')");
  10. stmt.addBatch("insert into dept2 values (53, '500', 'haha')");
  11. stmt.executeBatch();
  12. stmt.close();
  13. */
  14. PreparedStatement ps = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
  15. ps.setInt(1, 61);
  16. ps.setString(2, "haha");
  17. ps.setString(3, "bj");
  18. ps.addBatch();
  19. ps.setInt(1, 62);
  20. ps.setString(2, "haha");
  21. ps.setString(3, "bj");
  22. ps.addBatch();
  23. ps.setInt(1, 63);
  24. ps.setString(2, "haha");
  25. ps.setString(3, "bj");
  26. ps.addBatch();
  27. ps.executeBatch();
  28. ps.close();
  29. conn.close();
  30. }
  31. }

5 执行事务:

  1. import java.sql.*;
  2. public class TestTransaction {
  3. public static void main(String[] args) {
  4. Connection conn = null;
  5. Statement stmt = null;
  6. try {
  7. Class.forName("oracle.jdbc.driver.OracleDriver");
  8. conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:SXT", "scott", "tiger");
  9. conn.setAutoCommit(false);
  10. stmt = conn.createStatement();
  11. stmt.addBatch("insert into dept2 values (51, '500', 'haha')");
  12. stmt.addBatch("insert into dept2 values (52, '500', 'haha')");
  13. stmt.addBatch("insert into dept2 values (53, '500', 'haha')");
  14. stmt.executeBatch();
  15. conn.commit();
  16. conn.setAutoCommit(true);
  17. } catch (ClassNotFoundException e) {
  18. e.printStackTrace();
  19. } catch(SQLException e) {
  20. e.printStackTrace();
  21. try {
  22. if(conn != null)
  23. {
  24. conn.rollback();
  25. conn.setAutoCommit(true);
  26. }
  27. } catch (SQLException e1) {
  28. e1.printStackTrace();
  29. }
  30. }finally {
  31. try {
  32. if(stmt != null)
  33. stmt.close();
  34. if(conn != null)
  35. conn.close();
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }

6可以滚动的结果集:

  1. import java.sql.*;
  2. public class TestScroll {
  3. public static void main(String args[]) {
  4. try {
  5. new oracle.jdbc.driver.OracleDriver();
  6. String url = "jdbc:oracle:thin:@192.168.0.1:1521:SXT";
  7. Connection conn = DriverManager
  8. .getConnection(url, "scott", "tiger");
  9. Statement stmt = conn.createStatement(
  10. ResultSet.TYPE_SCROLL_INSENSITIVE,
  11. ResultSet.CONCUR_READ_ONLY);
  12. ResultSet rs = stmt
  13. .executeQuery("select * from emp order by sal");
  14. rs.next();
  15. System.out.println(rs.getInt(1));
  16. rs.last();
  17. System.out.println(rs.getString(1));
  18. System.out.println(rs.isLast());
  19. System.out.println(rs.isAfterLast());
  20. System.out.println(rs.getRow());
  21. rs.previous();
  22. System.out.println(rs.getString(1));
  23. rs.absolute(6);
  24. System.out.println(rs.getString(1));
  25. rs.close();
  26. stmt.close();
  27. conn.close();
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }

7可以更新的结果集:

  1. import java.sql.*;
  2. public class TestUpdataRs {
  3. public static void main(String args[]){
  4. try{
  5. new oracle.jdbc.driver.OracleDriver();
  6. String url="jdbc:oracle:thin:@192.168.0.1:1521:SXT";
  7. Connection conn=DriverManager.getConnection(url,"scott","tiger");
  8. Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
  9. ResultSet rs=stmt.executeQuery("select * from emp2");
  10. rs.next();
  11. //更新一行数据
  12. rs.updateString("ename","AAAA");
  13. rs.updateRow();
  14. //插入新行
  15. rs.moveToInsertRow();
  16. rs.updateInt(1, 9999);
  17. rs.updateString("ename","AAAA");
  18. rs.updateInt("mgr", 7839);
  19. rs.updateDouble("sal", 99.99);
  20. rs.insertRow();
  21. //将光标移动到新建的行
  22. rs.moveToCurrentRow();
  23. //删除行
  24. rs.absolute(5);
  25. rs.deleteRow();
  26. //取消更新
  27. //rs.cancelRowUpdates();
  28. }catch(SQLException e){
  29. e.printStackTrace();
  30. }
  31. }
  32. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/294493
推荐阅读
相关标签
  

闽ICP备14008679号