Jdbc标准编程

  1. package com.yelang;
  2. import java.sql.*;
  3. public class TestJdbc
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Connection conn = null;
  8.         Statement stmt = null;
  9.         ResultSet rs = null;
  10.         try
  11.         {
  12.             // Class.forName("com.mysql.jdbc.Driver").newInstance();
  13.             // new com.mysql.jdbc.Driver();
  14.             Class.forName("com.mysql.jdbc.Driver");
  15.             conn = DriverManager.getConnection(
  16.                     "jdbc:mysql://localhost:3306/shiyan""root""111");
  17.             stmt = conn.createStatement();
  18.             rs = stmt.executeQuery("select * from user");
  19.             while (rs.next())
  20.             {
  21.                 System.out.println(rs.getString("username"));
  22.                 System.out.println(rs.getString("password"));
  23.             }
  24.         }
  25.         catch (ClassNotFoundException e)
  26.         {
  27.             e.printStackTrace();
  28.         }
  29.         catch (SQLException e)
  30.         {
  31.             e.printStackTrace();
  32.         }
  33.         finally
  34.         {
  35.             try
  36.             {
  37.                 if (rs != null)
  38.                 {
  39.                     rs.close();
  40.                     rs = null;
  41.                 }
  42.                 if (stmt != null)
  43.                 {
  44.                     stmt.close();
  45.                     stmt = null;
  46.                 }
  47.                 if (conn != null)
  48.                 {
  49.                     conn.close();
  50.                     conn = null;
  51.                 }
  52.             }
  53.             catch (SQLException e)
  54.             {
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.     }
  59. }

 PreparedStatement

 

  1. import java.sql.*;
  2. public class TestPrepStmt
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         if (args.length != 3)
  7.         {
  8.             System.out.println("Parameter Error! Please Input Again!");
  9.             System.exit(-1);
  10.         }
  11.         int deptno = 0;
  12.         try
  13.         {
  14.             deptno = Integer.parseInt(args[0]);
  15.         }
  16.         catch (NumberFormatException e)
  17.         {
  18.             System.out
  19.                     .println("Parameter Error! Deptno should be Number Format!");
  20.             System.exit(-1);
  21.         }
  22.         String dname = args[1];
  23.         String loc = args[2];
  24.         PreparedStatement pstmt = null;
  25.         Connection conn = null;
  26.         try
  27.         {
  28.             Class.forName("oracle.jdbc.driver.OracleDriver");
  29.             // new oracle.jdbc.driver.OracleDriver();
  30.             conn = DriverManager.getConnection(
  31.                     "jdbc:oracle:thin:@192.168.0.1:1521:SXT""scott""tiger");
  32.             pstmt = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
  33.             pstmt.setInt(1, deptno);
  34.             pstmt.setString(2, dname);
  35.             pstmt.setString(3, loc);
  36.             pstmt.executeUpdate();
  37.         }
  38.         catch (ClassNotFoundException e)
  39.         {
  40.             e.printStackTrace();
  41.         }
  42.         catch (SQLException e)
  43.         {
  44.             e.printStackTrace();
  45.         }
  46.         finally
  47.         {
  48.             try
  49.             {
  50.                 if (pstmt != null)
  51.                 {
  52.                     pstmt.close();
  53.                     pstmt = null;
  54.                 }
  55.                 if (conn != null)
  56.                 {
  57.                     conn.close();
  58.                     conn = null;
  59.                 }
  60.             }
  61.             catch (SQLException e)
  62.             {
  63.                 e.printStackTrace();
  64.             }
  65.         }
  66.     }
  67. }

 

存储过程

 

SQL>create or replace procedure p
  2    (v_a in number,v_b number,v_ret out number,v_temp in out number)
  3 is
  4
  5 begin
  6    if(v_a > v_b) then
  7   v_ret := v_a;
  8    else
  9   v_ret := v_b;
  10   end if;
  11   v_temp := v_temp + 1;
  12 end;

 

  1. import java.sql.*;
  2. public class TestProc
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         Connection conn = null;
  7.         CallableStatement cstmt = null;
  8.         try
  9.         {
  10.             Class.forName("oracle.jdbc.driver.OracleDriver");
  11.             conn = DriverManager.getConnection(
  12.                     "jdbc:oracle:thin:@192.168.0.1:1521:SXT""scott""tiger");
  13.             cstmt = conn.prepareCall("{call p(?, ?, ?, ?)}");
  14.             cstmt.registerOutParameter(3, Types.INTEGER);
  15.             cstmt.registerOutParameter(4, Types.INTEGER);
  16.             cstmt.setInt(13);
  17.             cstmt.setInt(24);
  18.             cstmt.setInt(45);
  19.             cstmt.execute();
  20.             System.out.println(cstmt.getInt(3));
  21.             System.out.println(cstmt.getInt(4));
  22.         }
  23.         catch (Exception e)
  24.         {
  25.             e.printStackTrace();
  26.         }
  27.         finally
  28.         {
  29.             try
  30.             {
  31.                 if (cstmt != null)
  32.                 {
  33.                     cstmt.close();
  34.                     cstmt = null;
  35.                 }
  36.                 if (conn != null)
  37.                 {
  38.                     conn.close();
  39.                     conn = null;
  40.                 }
  41.             }
  42.             catch (SQLException e)
  43.             {
  44.                 e.printStackTrace();
  45.             }
  46.         }
  47.     }
  48. }

 

批处理

  1. import java.sql.*;
  2. public class TestBatch
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         Connection conn = null;
  7.         PreparedStatement ps = null;
  8.         try
  9.         {
  10.             Class.forName("oracle.jdbc.driver.OracleDriver");
  11.             conn = DriverManager.getConnection(
  12.                     "jdbc:oracle:thin:@192.168.0.1:1521:SXT""scott""tiger");
  13.             ps = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
  14.             ps.setInt(161);
  15.             ps.setString(2"haha");
  16.             ps.setString(3"bj");
  17.             ps.addBatch();
  18.             ps.setInt(162);
  19.             ps.setString(2"haha");
  20.             ps.setString(3"bj");
  21.             ps.addBatch();
  22.             ps.setInt(163);
  23.             ps.setString(2"haha");
  24.             ps.setString(3"bj");
  25.             ps.addBatch();
  26.             ps.executeBatch();
  27.         }
  28.         catch (Exception e)
  29.         {
  30.             e.printStackTrace();
  31.         }
  32.         finally
  33.         {
  34.             try
  35.             {
  36.                 if (ps != null)
  37.                 {
  38.                     ps.close();
  39.                     ps = null;
  40.                 }
  41.                 if (conn != null)
  42.                 {
  43.                     conn.close();
  44.                     conn = null;
  45.                 }
  46.             }
  47.             catch (Exception e)
  48.             {
  49.                 e.printStackTrace();
  50.             }
  51.         }
  52.     }
  53. }

 

Transaction,事务处理

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

 

JDBC处理可滚动的结果集

 

  1. import java.sql.*;
  2. public class TestScroll
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         Connection conn = null;
  7.         Statement stmt = null;
  8.         ResultSet rs = null;
  9.         try
  10.         {
  11.             Class.forName("oracle.jdbc.driver.OracleDriver");
  12.             String url = "jdbc:oracle:thin:@192.168.0.1:1521:SXT";
  13.             conn = DriverManager.getConnection(url, "scott""tiger");
  14.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  15.                     ResultSet.CONCUR_READ_ONLY);
  16.             rs = stmt.executeQuery("select * from emp order by sal");
  17.             rs.next();
  18.             System.out.println(rs.getInt(1));
  19.             rs.last();
  20.             System.out.println(rs.getString(1));
  21.             System.out.println(rs.isLast());
  22.             System.out.println(rs.isAfterLast());
  23.             System.out.println(rs.getRow());
  24.             rs.previous();
  25.             System.out.println(rs.getString(1));
  26.             rs.absolute(6);
  27.             System.out.println(rs.getString(1));
  28.         }
  29.         catch (ClassNotFoundException e)
  30.         {
  31.             e.printStackTrace();
  32.         }
  33.         catch (SQLException e)
  34.         {
  35.             e.printStackTrace();
  36.         }
  37.         finally
  38.         {
  39.             try
  40.             {
  41.                 if (rs != null)
  42.                 {
  43.                     rs.close();
  44.                     rs = null;
  45.                 }
  46.                 if (stmt != null)
  47.                 {
  48.                     stmt.close();
  49.                     stmt = null;
  50.                 }
  51.                 if (conn != null)
  52.                 {
  53.                     conn.close();
  54.                     conn = null;
  55.                 }
  56.             }
  57.             catch (SQLException e)
  58.             {
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.     }
  63. }

 

JDBC处理可更新的结果集

 

  1. import java.sql.*;
  2. public class TestUpdataRs
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         Connection conn = null;
  7.         Statement stmt = null;
  8.         ResultSet rs = null;
  9.         try
  10.         {
  11.             Class.forName("oracle.jdbc.driver.OracleDriver");
  12.             String url = "jdbc:oracle:thin:@192.168.0.1:1521:SXT";
  13.             conn = DriverManager.getConnection(url, "scott""tiger");
  14.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  15.                     ResultSet.CONCUR_UPDATABLE);
  16.             rs = stmt.executeQuery("select * from emp2");
  17.             rs.next();
  18.             // 更新一行数据
  19.             rs.updateString("ename""AAAA");
  20.             rs.updateRow();
  21.             // 插入新行
  22.             rs.moveToInsertRow();
  23.             rs.updateInt(19999);
  24.             rs.updateString("ename""AAAA");
  25.             rs.updateInt("mgr"7839);
  26.             rs.updateDouble("sal"99.99);
  27.             rs.insertRow();
  28.             // 将光标移动到新建的行
  29.             rs.moveToCurrentRow();
  30.             // 删除行
  31.             rs.absolute(5);
  32.             rs.deleteRow();
  33.             // 取消更新
  34.             // rs.cancelRowUpdates();
  35.         }
  36.         catch (ClassNotFoundException e)
  37.         {
  38.             e.printStackTrace();
  39.         }
  40.         catch (SQLException e)
  41.         {
  42.             e.printStackTrace();
  43.         }
  44.         finally
  45.         {
  46.             try
  47.             {
  48.                 if (rs != null)
  49.                 {
  50.                     rs.close();
  51.                     rs = null;
  52.                 }
  53.                 if (stmt != null)
  54.                 {
  55.                     stmt.close();
  56.                     stmt = null;
  57.                 }
  58.                 if (conn != null)
  59.                 {
  60.                     conn.close();
  61.                     conn = null;
  62.                 }
  63.             }
  64.             catch (SQLException e)
  65.             {
  66.                 e.printStackTrace();
  67.             }
  68.         }
  69.     }
  70. }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值