JDBC

来源:互联网 发布:windows 系统编程 编辑:程序博客网 时间:2024/05/21 19:46

package com.founder.record.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @时间: Oct 31, 2006 10:45:27 AM
 * @类说明:数据库操作类
 */
public class DBController
{
 private static String url = PropertyFile.getValue("db.url");
 private static String userName = PropertyFile.getValue("db.name");
 private static String userPwd = PropertyFile.getValue("db.password");
 private Connection conn = null;
 private PreparedStatement pstmt_insert = null;
 private String SQL_INSERT = "insert into student (id,name,address) values (?,?,?)";

 public static void main(String[] args) throws SQLException
 {
  DBController dbc = new DBController();
  dbc.getPreparedStatement();
 }

 public void getPreparedStatement() throws SQLException
 {
  try
  {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   conn = DriverManager.getConnection(url, userName, userPwd);
   this.pstmt_insert = this.conn.prepareStatement(SQL_INSERT);
   for (int i = 0; i < 100; i++)
   {
    this.pstmt_insert.setInt(1, i);
    this.pstmt_insert.setString(2, "刘效勤" + i);
    this.pstmt_insert.setString(3, "上地" + i + "号");
    this.pstmt_insert.execute();
    this.pstmt_insert.clearParameters();
    System.out.println("插入" + i + "条");
   }

  } catch (Exception e)
  {
  } finally
  {
   if (this.pstmt_insert != null)
   {
    this.pstmt_insert.close();
   }
   if (this.conn != null)
   {
    this.conn.close();
   }
  }
 }
}

 

 

-------------PropertyFile------------------

 

 

 

package com.founder.record.db;

import java.util.ResourceBundle;

/**
 * @时间: Oct 31, 2006 10:47:13 AM
 * @类说明:获取配置文件内容
 */
public class PropertyFile
{
 public static String getValue(String key)
 {
  String temp = ResourceBundle.getBundle("system").getString(key);
  return temp;
 }
}

原创粉丝点击