原生servlet——如何连接数据库
发布日期:2021-05-15 00:36:57 浏览次数:20 分类:精选文章

本文共 6335 字,大约阅读时间需要 21 分钟。

������������������������������

��������������������� JDBC ���������������������������������������������������������������������������������CRUD������������������

������������������

���������������������������������������������������������

com
��������� dao
��� ��������� impl
��� ��� ��������� BaseDao.java
��� ��� ��������� StudentDao.java
��� ��������� model
��� ��������� entity
��� ��� ��������� Student.java
��� ��������� domain
��� ��������� StudentDomain.java

���������������������

������ jdbc.properties ���������������������������������

username=root
password=1234
url=jdbc:mysql://localhost:3306/peixun?characterEncoding=UTF-8
driverClassName=com.mysql.jdbc.Driver

������������������������������

���������������������������������������������������������

package com.dao;
public interface BaseDao
{
T selectForOne(Class, String, Object... args);
List selectForMore(Class, String, Object... args); int update(String, Object... args); }

������������������

������������������������

  • ���������������������
  • mkdir -p src/main/java/com/dao/impl
    mkdir -p src/main/java/com/model/entity

    ���������������������

  • ������ jdbc.properties���
  • username=root
    password=1234
    url=jdbc:mysql://localhost:3306/peixun
    driverClassName=com.mysql.jdbc.Driver
    1. ������ JdbcUtils ������������
    2. package com.utils;
      import com.mysql.jdbc.Driver;
      import org.apache.commons.dbutils.QueryRunner;
      import org.apache.commons.dbutils.handlers.BeanHandler;
      import org.apache.commons.dbutils.handlers.BeanListHandler;
      import org.springframework.jdbc.datasource.DriverManagerDataSource;
      import java.sql.DriverManager;
      import java.sql.SQLException;
      public class JdbcUtils {
      private static ThreadLocal
      dataSource = new ThreadLocal<>();
      static {
      try {
      ClassLoader classLoader = JdbcUtils.class.getClassLoader();
      InputStream is = classLoader.getResourceAsStream("jdbc.properties");
      Properties props = new Properties();
      props.load(is);
      dataSource.set((DriverManagerDataSource) DriverManager.createDataSource(props));
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      public static Connection getConnection() {
      try {
      return dataSource.get().getConnection();
      } catch (SQLException e) {
      throw new RuntimeException("���������������������������", e);
      }
      }
      public static void closeConnection(Connection connection) {
      try {
      if (connection != null) {
      connection.close();
      }
      } catch (SQLException e) {
      e.printStackTrace();
      }
      }
      }

      ������������������ BaseDao ���

      package com.dao.impl;
      import com.dao.BaseDao;
      import com.model.entity.Student;
      import org.apache.commons.dbutils.QueryRunner;
      import org.apache.commons.dbutils.handlers.BeanHandler;
      import org.apache.commons.dbutils.handlers.BeanListHandler;
      import com.utils.JdbcUtils;
      import java.util.List;
      import java.sql.Connection;
      import java.sql.SQLException;
      public class BaseDao
      implements BaseDao
      {
      private QueryRunner queryRunner = new QueryRunner();
      public T selectForOne(Class
      type, String sql, Object... args) {
      Connection connection = JdbcUtils.getConnection();
      try {
      return getBean(type, sql, args);
      } catch (SQLException e) {
      throw new RuntimeException("������������������������", e);
      } finally {
      JdbcUtils.closeConnection(connection);
      }
      }
      public List
      selectForMore(Class
      type, String sql, Object... args) {
      Connection connection = JdbcUtils.getConnection();
      try {
      return listBeans(type, sql, args);
      } catch (SQLException e) {
      throw new RuntimeException("������������������������", e);
      } finally {
      JdbcUtils.closeConnection(connection);
      }
      }
      private T getBean(Class
      type, String sql, Object... args) { try { return queryRunner.query(JdbcUtils.getConnection(), sql, new BeanHandler
      (type), args); } catch (SQLException e) { throw new RuntimeException("������������������", e); } } private List
      listBeans(Class
      type, String sql, Object... args) { try { return queryRunner.query(JdbcUtils.getConnection(), sql, new BeanListHandler
      (type), args); } catch (SQLException e) { throw new RuntimeException("������������������", e); } } public int update(String sql, Object... args) { Connection connection = JdbcUtils.getConnection(); try { return queryRunner.update(connection, sql, args); } catch (SQLException e) { throw new RuntimeException("������������������������", e); } finally { JdbcUtils.closeConnection(connection); } return -1; } }

      ��������� StudentDao ��������� BaseDao

      package com.dao;
      public interface StudentDao extends BaseDao
      {
      }

      ������������ StudentDao

      package com.dao.impl;
      import com.dao.StudentDao;
      import com.model.entity.Student;
      import com.utils.JdbcUtils;
      public class StudentDao implements StudentDao {
      }

      ������������

    3. ������������������
    4. package com;
      import com.dao.impl.BaseDao;
      import com.dao.StudentDao;
      import com.model.entity.Student;
      import org.junit.jupiter.api.Test;
      import static org.junit.jupiter.api.Assertions.*;
      class TestDataAccess {
      @Test
      void testSelectMore() {
      BaseDao
      baseDao = new BaseDao<>();
      List
      students = baseDao.selectForMore(Student.class, "SELECT * FROM student");
      assertTrue(students.size() > 0);
      for (Student student : students) {
      assertEquals("St", student.getName());
      }
      }
      @Test
      void testUpdate() {
      BaseDao
      baseDao = new BaseDao<>();
      Student student = new Student();
      student.setName("X");
      int updated = baseDao.update("UPDATE student SET name = ?", student.getName());
      assertEquals(1, updated);
      }
      }

      ������������

      ��������������������������� Student.java ��� jdbc.properties ������������������������������������������������������������������������������������������

      ���������������������������������������������������������������������������������������������������������������������������

    上一篇:Maven idela 建立一个javaweb项目
    下一篇:云服务器搭建好,出现 拒绝了我们的连接请求。

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月24日 04时01分40秒