
原生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=rootpassword=1234url=jdbc:mysql://localhost:3306/peixun?characterEncoding=UTF-8driverClassName=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/implmkdir -p src/main/java/com/model/entity
���������������������
jdbc.properties
���username=rootpassword=1234url=jdbc:mysql://localhost:3306/peixundriverClassName=com.mysql.jdbc.Driver
- ������
JdbcUtils
������������ - ������������������
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 ThreadLocaldataSource = 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 BaseDaoimplements 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 {}
������������
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() { BaseDaobaseDao = 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
������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月24日 04时01分40秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Explore Optimization
2021-05-10
Kali Linux 内网渗透教程 - ARP欺骗攻击 | 超详细
2021-05-10
2020Java程序设计基础(华东交通大学)章节测试免费满分答案
2021-05-10
解决数据库报ORA-02289:序列不存在错误
2021-05-10
map[]和map.at()取值之间的区别
2021-05-11
成功解决升级virtualenv报错问题
2021-05-11
【SQLI-Lab】靶场搭建
2021-05-11
【Bootstrap5】精细学习记录
2021-05-11
Struts2-从值栈获取list集合数据(三种方式)
2021-05-11
vscode中快速生成vue模板
2021-05-11
参考图像
2021-05-12
设计模式(18)——中介者模式
2021-05-12
推荐几篇近期必看的视觉综述,含GAN、Transformer、人脸超分辨、遥感等
2021-05-12
BUU-MISC-认真你就输了
2021-05-12
BUU-MISC-caesar
2021-05-12
【专题2:电子工程师 之 上位机】 之 【36.事件重载】
2021-05-12