MySQL 数据库 (实现JDBC工具类)

JDBC工具类:

package com.itcast.ma;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;


/**

* 工具类

* 提供获取连接及释放资源的方法

* @author lenovo

*

*/


public class JDBCUtils {

    /**

     * 获取连接方法

     * @return

     */

    public static Connection getConnection(){

        Connection conn = null;

        try {

            Class.forName("com.mysql.jdbc.Driver");

            

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","123456");

            

        } catch (Exception e) {

            

            e.printStackTrace();

        }

        

        

        return conn;

    }

    /**

     * 释放资源方法

     * @param conn

     * @param pstmt

     * @param rs

     */

    public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs){

        if(rs != null){

            try {

                rs.close();

            } catch (SQLException e) {

                

                e.printStackTrace();

            }

        }

        if(pstmt != null){

            try {

                pstmt.close();

            } catch (SQLException e) {

                

                e.printStackTrace();

            }

        }

        if(conn != null){

            try {

                conn.close();

            } catch (SQLException e) {

                

                e.printStackTrace();

            }

        }

    }


}

查询数据:

package com.ma.test;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import org.junit.Test;

import com.itcast.ma.JDBCUtils;

/**

 *测试工具类

 * @author lenovo

 *

 */

public class TestUtils {

      /**

       * 根据id查询

       */

      @Test

      public void testFindUserById(){

            Connection conn = null;

            PreparedStatement pstmt  = null;

            ResultSet rs = null;

      

            try {

                  //1、获取连接

                  conn = JDBCUtils.getConnection();

                  //2、编写 sql

                  String sql = "select * from t_user where uid = ?";

                  //3、获取执行语句对象

                  pstmt = conn.prepareStatement(sql);

                  //4、设置参数

                  pstmt.setInt(1, 2);

                  //5、查询

                  rs = pstmt.executeQuery();

                  //6、处理资源

                  while(rs.next()){

                        System.out.println(rs.getString(2)+"----"+ rs.getString("password"));

                  }

                  

            } catch (SQLException e) {

                  

                  e.printStackTrace();

            }finally{

                  //7、释放资源

                  JDBCUtils.release(conn, pstmt, rs);

            }

            

      } 

}

测试结果:

使用 properties 配置文件:

开发过程中获得连接的四个参数(驱动、url 、用户名、密码)通常都存放在配置文件中,方便后期维护,程序如果需要更换数据库,只需要修改配置文件即可。

1、文件位置:建议 src 下面。

2、文件名称:任意,扩展名为 properties。

3、文件内容:一行一组数据,格式是“key = value”;key 名自定义,如果多个单词用 . 分隔;value 不支持中文。

driver = com.mysql.jdbc.Driver

url = jdbc;mysql://localhost:3306/user?useUnicode = true & characterEncoding = utf-8

username = rooot

password = 123456

加载配置文件:ResourceBundle 对象。

package com.itcast.ma;

import java.util.ResourceBundle;

/**

 * 工具类

 * 提供获取连接及释放资源的方法

 * @author lenovo

 *

 */

public class JDBCUtils {

      private static String driver;//驱动

      private static String url;//路径

      private static String user;//用户

      private static String password;//密码

      /**

       * 配置文件只需要加载一次,提供静代码,当前类被加载到内存执行

       */

      static{

            //1、使用 JDK 提供的工具类加载 properties 文件

            //ResourceBundle 专门处理 properties 文件,提供 getBundle() 方法,只需要写入文件。

            ResourceBundle bundle = ResourceBundle.getBundle("db");

            //2、通过 key 获取需要的值

            driver = bundle.getString("driver");

            url = bundle.getString("url");

            user = bundle.getString("username");

            password = bundle.getString("password");

            

      }

}

插入数据:


 

/**

       * 添加用户信息方法

       */

      @Test

      public void testAdd(){

            Connection conn = null;

            PreparedStatement pstmt = null;

            

            try {

                  //1、获取连接

                  conn = JDBCUtils.getConnection();

                  //2、书写 sql 语句

                  String sql = "insert into t_user values(null,?,?,?)";

                  //3、获取 sql 执行对象

                  pstmt = conn.prepareStatement(sql);

                  //4、设置参数

                  pstmt.setString(1, "MaYue");

                  pstmt.setString(2, "321456");

                  pstmt.setString(3, "LIN");

                  //5、执行插入操作

                  int row = pstmt.executeUpdate();

                  if(row > 0){

                        System.out.println("插入成功 !");

                  }

            } catch (SQLException e) {

                  

                  e.printStackTrace();

            }

            

      }

删除数据:

/**

       * 根据id 删除用户的方法

       */

      @Test

      public void testDelete(){

            Connection conn = null;

            PreparedStatement pstmt = null;

            

            try {

                  //1、获取连接

                  conn = JDBCUtils.getConnection();

                  //2、书写 sql 语句

                  String sql = "delete from t_user where uid = ?";

                  //3、获取 sql 执行对象

                  pstmt = conn.prepareStatement(sql);

                  //4、设置参数

                  pstmt.setInt(1, 9);

                  //5、执行插入操作

                  int row = pstmt.executeUpdate();

                  if(row > 0){

                        System.out.println("删除成功 !");

                  }else{

                        System.out.println("删除失败 !");

                  }

            } catch (SQLException e) {

                  

                  e.printStackTrace();

            }finally{

                  //6、释放资源

                  JDBCUtils.release(conn, pstmt, null);

            }

            

            

      }


 

更新数据:

/**

       * 根据id 更新用户的方法

       */

      @Test

      public void testUpdate(){

            Connection conn = null;

            PreparedStatement pstmt = null;

            

            try {

                  //1、获取连接

                  conn = JDBCUtils.getConnection();

                  //2、书写 sql 语句

                  String sql = "update t_user set password = ? where uid = ?";

                  //3、获取 sql 执行对象

                  pstmt = conn.prepareStatement(sql);

                  //4、设置参数

                  pstmt.setString(1, "666666");

                  pstmt.setInt(2, 1 );

                  //5、执行插入操作

                  int row = pstmt.executeUpdate();

                  if(row > 0){

                        System.out.println("更新成功 !");

                  }else{

                        System.out.println("更新失败 !");

                  }

            } catch (SQLException e) {

                  

                  e.printStackTrace();

            }finally{

                  //6、释放资源

                  JDBCUtils.release(conn, pstmt, null);

            }

            

            

      }

猜你喜欢

转载自blog.csdn.net/young_1004/article/details/82057398