写数据库

晚饭过后,终于有了点精神,顺便把写数据库给做了,这么小的程序就用jdbc吧! 呵呵省力!!!

首先引进import com.cims.db.*;包,其中封装了两个类,conn(创立连接),rs(执行sql)

呵呵接着在 row 循环里续代码了

  /**

   *  below i will connection the database and execute the sql .

              **/

              try

              { 

                    System.out.println("Start connet to db");

                   

                   

                   

                    resultset.execute(sql.toString());

              }catch(Exception e)

              {

                   

                    System.out.println("  sql exception "+e);

              }

就这几行了,所有的数据库操作已经简单封装在被引用的包里。 呵呵  如果您在看我的日记 不是很名白com.cims.db.*

拿酒来看看它中的代码吧!

  conn:  一个创立连接的类  它提供了getConnection方法返回一个connection

  public Connection getConnection() throws SQLException,IOException

{  

String url=null;

String username=null;

    String password=null;

   Connection conn=null;

   Properties props=new Properties();  

   InputStream in = getClass().getResourceAsStream("/database.properties");

   props.load(in);

   System.out.println("hello2");

   in.close();    

    try{

String drivers=props.getProperty("jdbc.drivers");

System.out.println(drivers);

if (drivers!=null)

    Class.forName(drivers).newInstance();

    System.out.println("test for url");

url=props.getProperty("jdbc.url");

System.out.println(url);

username=props.getProperty("username");

password=props.getProperty("password");

System.out.println(password);

   }

   catch(Exception e){System.out.println(e.getMessage());}

   return DriverManager.getConnection(url,username,password);

}

   Rs :sql执行类 其中

      public ResultSet executeQuery(String sql) throws SQLException,IOException方法用于

     数据库查询

      public int executeUpdate(String sql) throws SQLException,IOException 用于数据更新

     当然,还用更通用的方法

     public boolean execute (String sql) throws SQLException,IOException 让他来完成我们的

    数据插入

   看看conn 你就会发现使用jdbc怎么配置数据源了 呵呵  来看看MySQL数据库下配置数据源 

   database.properties的代码:

     jdbc.drivers=org.gjt.mm.mysql.Driver

     jdbc.url=jdbc:mysql://localhost:3306/crm

     username=root

     password=cimserp

  如果你使用的是oracle, 拿酒看看下面例子

    jdbc.drivers=oracle.jdbc.driver.OracleDriver

    jdbc.url=jdbc:oracle:thin:@10.11.12.46:1521:cimserp

    username=cimserp

    password=cimserp

  好了轻轻松松的就将excel中的数据插入数据库了,poi的测试也就到此结束!!我也可以给我朋友说,no problem,

  你需要的很快竟能完成,:)

  看看还需要足作些什么  哦!!  后面几天将把它移植到struts框架下,将用eclipse 2.1 版本+easystruts 快速开发