JDBC Link database version three, using C3P0, using jar files of two

Source: Internet
Author: User

Jdbcutil class:

[Java]View PlainCopy
  1. Package com.xiaohui.jdbc.util;
  2. Import java.sql.Connection;
  3. Import java.sql.PreparedStatement;
  4. Import Java.sql.ResultSet;
  5. Import java.sql.SQLException;
  6. Import Javax.sql.DataSource;
  7. Import Com.mchange.v2.c3p0.ComboPooledDataSource;
  8. Public Final class Jdbcutil {
  9. private static Combopooleddatasource DataSource;
  10. Static {
  11. DataSource = new Combopooleddatasource ();
  12. }
  13. //Get links
  14. public static Connection getmysqlconnection () throws SQLException {
  15. return datasource.getconnection ();
  16. }
  17. //  
  18. public static DataSource Getdatasource () {
  19. return dataSource;
  20. }
  21. //Close link
  22. public static void Close (Connection conn) throws SQLException {
  23. if (conn! = null) {
  24. try {
  25. Conn.close ();
  26. } catch (SQLException e) {
  27. E.printstacktrace ();
  28. throw E;
  29. }
  30. }
  31. }
  32. public static void Close (PreparedStatement pstate) throws SQLException {
  33. if (pstate!=null) {
  34. Pstate.close ();
  35. }
  36. }
  37. public static void Close (ResultSet rs) throws SQLException {
  38. if (rs!=null) {
  39. Rs.close ();
  40. }
  41. }
  42. }

C3p0-config.xml [HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <c3p0-config>
  3. <default-config>
  4. < name="Driverclass">com.mysql.jdbc.driver</ Property >
  5. < name="user">root </Property>
  6. < name="password">root </Property>
  7. < name="Jdbcurl">jdbc:mysql://127.0.0.1:3306/mysql4</ Property >
  8. </default-config>
  9. </c3p0-config>


One DAO for pagination: [Java]View PlainCopy
  1. Package Com.xiaohui.cusSys.dao;
  2. Import java.sql.SQLException;
  3. Import java.util.List;
  4. Import Org.apache.commons.dbutils.QueryRunner;
  5. Import Org.apache.commons.dbutils.handlers.BeanHandler;
  6. Import Org.apache.commons.dbutils.handlers.BeanListHandler;
  7. Import Org.apache.commons.dbutils.handlers.ScalarHandler;
  8. Import Com.xiaohui.cusSys.domain.Customer;
  9. Import Com.xiaohui.cusSys.util.JdbcUtil;
  10. Public class Dao {
  11. private Queryrunner qr = new Queryrunner (Jdbcutil.getdatasource ());
  12. //Return Customer object by ID
  13. Public Customer Getcustomerbyid (int id) throws SQLException {
  14. String sql = "SELECT * FROM customer where id =?";
  15. Customer cus = (customer) qr.query (SQL,
  16. New Beanhandler (Customer.   Class), id);
  17. return cus;
  18. }
  19. //Paging back
  20. Public list<customer> getfylist (int start, int size) throws SQLException {
  21. list<customer> list = null;
  22. String sql = "SELECT * FROM Customer limit?,?";
  23. List = Qr.query (sql, new Beanlisthandler (Customer. Class), new object[] {
  24. Start, size});
  25. return list;
  26. }
  27. //Returns the total number of records
  28. public int Getallrecordscount () throws SQLException {
  29. String sql = "SELECT COUNT (*) from customer";
  30. Long temp = qr.query (sql, new Scalarhandler ());
  31. return Temp.intvalue ();
  32. }
  33. //Delete the specified records by ID
  34. public void Deleterecordbyid (int id) throws SQLException {
  35. String sql = "Delete from customer where id =?";
  36. Qr.update (SQL, id);
  37. }
  38. //update record information by ID
  39. public void Updaterecordbyid (Customer newcus) throws SQLException {
  40. String sql = "Update customer set name=?", address=?, tel=?, mail=?, birthday=? where id=? ";
  41. Qr.update (
  42. Sql
  43. New object[] {newcus.getname (), newcus.getaddress (),
  44. Newcus.gettel (), Newcus.getmail (),
  45. Newcus.getbirthday (), Newcus.getid ()});
  46. }
  47. //Add a record
  48. public void AddRecord (Customer newcus) throws SQLException {
  49. String sql = "INSERT into customer (Name,address,tel,mail,birthday) VALUES (?,?,?,?,?)";
  50. Qr.update (SQL, new object[] {newcus.getname (), newcus.getaddress (),
  51. Newcus.gettel (), Newcus.getmail (),
  52. //// convert java.util.Date to Java.sql.Date
  53. //New Java.sql.Date (Newcus.getbirthday (). GetTime ())
  54. Newcus.getbirthday ()});
  55. }
  56. }


Jar file: C3p0-0.9.1.2.jar (Critical) Mysql-connector-java-5.1.22-bin.jar (critical) used in DAO to Commons-dbutils-1.5.jar
Top

JDBC Link database version three, using C3P0, using jar files of two

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.