性格决定命运,气度影响格局
posts - 20, comments - 18, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

JDBC简单举例

Posted on 2007-08-04 16:10 尚爱军 阅读(311) 评论(2)  编辑  收藏

例子1:
****************************************************************
说明:仅仅实现了数据库的连接,测试了一条查询语句                              *
****************************************************************
import java.sql.*;

public class TestJDBC {


 public static void main(String[] args) {
  ResultSet rs = null;
  Statement stmt = null;
  Connection conn = null;
    
  String driver = "oracle.jdbc.driver.OracleDriver";   
  String url = "jdbc:oracle:thin:@ROBUST:1521:ORACLE9";  
  String userName = "scott";
  String userPassword = "tiger";
 
  try {
   Class.forName(driver);//加载驱动程序。
   conn = DriverManager.getConnection(url, userName, userPassword);//获得连接。
   stmt = conn.createStatement();                                                                  //获得statement
   rs = stmt.executeQuery("select * from dept");
   while(rs.next()) {
    System.out.println(rs.getString("deptno"));
    System.out.println(rs.getInt("deptno"));
    System.out.println(rs.getString("dname"));
   }
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    if (rs != null) {
     rs.close();
     rs = null;
    }
    
    if (stmt != null) {
     stmt.close();
     stmt = null;
    }
    if (conn != null) {
     conn.close();
     conn = null;
    }

   } catch (SQLException e) {
    e.printStackTrace();
   }

  }

 }

}

***************************************************************
说明:测试了一条插入语句                                                                            *
***************************************************************
例子2:
import java.sql.*;
public class TestDML {

 
 public static void main(String[] args) {
 String driver = "oracle.jdbc.driver.OracleDriver"; 
 String url = "jdbc:oracle:thin:@robust:1521:oracle9";
 String userName = "scott";
 String userPassword = "tiger";
 String sql = "insert into dept values(50,'Shang','Harbin')";
 
 Connection conn = null;
 Statement stmt = null;
 
 try {
  Class.forName(driver);
  conn = DriverManager.getConnection(url, userName, userPassword);
  stmt = conn.createStatement();
  stmt.executeUpdate(sql);
  
 } catch (ClassNotFoundException e) {
  e.printStackTrace();
 } catch (SQLException e) {
  e.printStackTrace();  
 } finally {
  try {

   if(stmt != null) {
    stmt.close();
    stmt = null;
   }
    
   if(conn != null) {
    conn.close();
    conn = null;
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }

 }
 

 }

}
举例3
********************************************************************
说明:通过命令行输入实现数据库的插入,注意sql语句的写法                         *
********************************************************************                  
import java.sql.*;

public class TestDML2 {


 public static void main(String[] args) {
  String driver = "oracle.jdbc.driver.OracleDriver";
  String url = "jdbc:oracle:thin:@robust:1521:oracle9";
  String userName = "scott";
  String userPassword = "tiger";
  int deptno = 0;
  String dname = args[1];
  String loc = args[2];
    
  if(args.length != 3) {
   System.out.println("参数个数输入错误,请重新运行程序!");
   System.exit(-1);
  }
  try{
   deptno = Integer.parseInt(args[0]);
  } catch (NumberFormatException e) {
   System.out.println("参数输入错误,请输入数字!");
   System.exit(-1);
  }
  String sql = "insert into dept values (" + deptno + ",'" + dname + "','" + loc + "')";
  
  
//System.out.println(sql);  
  
  Connection conn = null;
  Statement stmt = null;
  try{
   Class.forName(driver);
   conn = DriverManager.getConnection(url, userName, userPassword);
   stmt = conn.createStatement();
   stmt.executeUpdate(sql);
  }catch (ClassNotFoundException e) {
   e.printStackTrace();
  }catch (SQLException e) {
   e.printStackTrace();
  }finally {
   try {
    if(stmt != null) {
     stmt.close();
     stmt = null;
    }
    if(conn != null) {
     conn.close();
     conn = null;
    }
   } catch (SQLException e) {
    e.printStackTrace();
   }
   

   
  }
 }

}

例子4:
********************************************************************
说明:通过命令行输入实现数据库的插入,改用了PreparedStatement           *
********************************************************************  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;

public class TestPreparedStmt {


 public static void main(String[] args) {
  String driver = "oracle.jdbc.driver.OracleDriver";
  String url = "jdbc:oracle:thin:@robust:1521:oracle9";
  String userName = "scott";
  String userPassword = "tiger";
  int deptno = 0;
  String dname = args[1];
  String loc = args[2];
    
  if(args.length != 3) {
   System.out.println("参数个数输入错误,请重新运行程序!");
   System.exit(-1);
  }
  try{
   deptno = Integer.parseInt(args[0]);
  } catch (NumberFormatException e) {
   System.out.println("参数输入错误,请输入数字!");
   System.exit(-1);
  }
  
  
  
//System.out.println(sql);  
  
  Connection conn = null;
  PreparedStatement pstmt = null;
  try{
   Class.forName(driver);
   conn = DriverManager.getConnection(url, userName, userPassword);
   pstmt = conn.prepareStatement("insert into dept values (?,?,?)");
   pstmt.setInt(1, deptno);
   pstmt.setString(2, dname);
   pstmt.setString(3, loc);               //1 2 3 表示第1 2 3 个位置。
   pstmt.executeUpdate();

  }catch (ClassNotFoundException e) {
   e.printStackTrace();
  }catch (SQLException e) {
   e.printStackTrace();
  }finally {
   try {
    if(pstmt != null) {
     pstmt.close();
     pstmt = null;
    }
    if(conn != null) {
     conn.close();
     conn = null;
    }
   } catch (SQLException e) {
    e.printStackTrace();
   }
   

   
  }
 }

}

例子5:
****************************************************************
说明:java如何调用存储过程的举例。                                                           *
****************************************************************
***************************************************************************  
   create or replace procedure p                                                                                             *
          ( v_a int number , v_b number ,v_ret out number, v_temp int out number )   *
      is                                                                                                                                           *

     begin                                                                                                                                     *
            if (v_a > v_b) then                                                                                                      *
                v_ret :=v_a;                                                                                                             *
            else                                                                                                                                 *
               v_ret := v_b;                                                                                                             *
            end if;                                                                                                                             *
            v_temp := v_temp + 1;                                                                                              *
      end;                                                                                                                                   *
***************************************************************************
sql语句如上。

import java.sql.*;
public class TestProc {

 /**
  * @param args
  */
 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();
 }

}

例子6:
****************************************************************
说明:批处理举例                                                                                              *
****************************************************************
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();

 }

}

例子7:
****************************************************************
说明:提交的例子                                                                                             *
****************************************************************
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);   //将自动提交设置为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();                       //自动提交为false,所以只能手动提交了。
   conn.setAutoCommit(true); 
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch(SQLException e) {
   
   e.printStackTrace();
   
   try {
    if(conn != null)
    {
     conn.rollback();
     conn.setAutoCommit(true);   //因为这里面是SQLException,说明sql语句出错了,必须回滚。
    }
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
  }finally {
   try {
    if(stmt != null)
     stmt.close();
    if(conn != null)
     conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  

 }

}
例子8:
************************************************************************
JDBC处理可滚动的结果集                                                                                                *
************************************************************************
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();
  }
 }
}


评论

# re: JDBC简单举例[未登录]  回复  更多评论   

2008-07-03 18:14 by yue
很全面

# re: JDBC简单举例  回复  更多评论   

2008-07-04 09:24 by owner
哈哈,谢谢夸奖,好久没有动静了

只有注册用户登录后才能发表评论。


网站导航: