码迷,mamicode.com
首页 > 数据库 > 详细

数据库操作类

时间:2014-10-01 01:38:10      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   os   ar   strong   for   数据   sp   

对于JDBC操作数据库步骤:

  1.导入驱动包

  2.加载驱动(对于mysql数据库,则是Class.forName("com.mysql.jdbc.driver"),对于sql serverl数据库,则是Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"))

   不清楚驱动类,可以到jar包找找类似"Driver"字样,就能找到。

  3.获得Connection

 

  4.获得StatementPreparedStatement,执行executeUpdate()executeQuery()方法,返回结果集ResultSet或影响行数int。

  5.进行关闭操作,关闭ResultSet,关闭Statement,关闭Connection。

第2步一次就够了,第3.4.5步循环操作,所以操作类只实例一次时,不要将第三步放到放到构造方法中。

  参考例子:

    例子1:

    private static final String dbURL="jdbc:mysql://localhost:3306/test?user=root&password=admin&useUnicode=true&characterEncoding=utf-8";

  this.conn=DriverManager.getConnection(dbURL);

    例子2:

   private static final String dbURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
   private static final String user="root";
   private static final String password="admin";

   this.conn=DriverManager.getConnection(dbURL, user, password);

   例子3:

    private static final String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=db_test;user=sa;password=123456";

 

数据库操作类

标签:style   color   io   os   ar   strong   for   数据   sp   

原文地址:http://www.cnblogs.com/lipengsheng-javaweb/p/4002965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!