• Java工具类-Jdbc工具类


    简单玩一下:

    1.pom.xml文件配置

    1.         org.postgresql
    2.         postgresql
    3.         42.2.2
    4. org.apache.commons
    5. commons-dbcp2
    6. provided

     2.工具类

    1. import java.sql.*;
    2. /**
    3. * 类名称:
    4. *
    5. * @author 李庆伟
    6. * @date 2023年10月23日 10:48
    7. */
    8. public class JdbcUtil {
    9. private static final String DRIVER_NAME="org.postgresql.Driver";
    10. //连接数据的URL路径
    11. private static final String URL="jdbc:postgresql://localhost:5432/haha";
    12. //数据库登录账号
    13. private static final String USERNAME="postgres";
    14. //数据库登录密码
    15. private static final String PASSWORD="RBt47*XHy43GbH34";
    16. static{
    17. try {
    18. Class.forName(DRIVER_NAME);
    19. } catch (ClassNotFoundException e) {
    20. throw new ExceptionInInitializerError(e);
    21. }
    22. }
    23. //创建连接
    24. public static Connection getConnection(){
    25. try {
    26. return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    27. } catch (SQLException e) {
    28. e.printStackTrace();
    29. }
    30. return null;
    31. }
    32. //查找
    33. public static ResultSet find(String sql) {
    34. Connection conn = JdbcUtil.getConnection();
    35. try {
    36. conn.setAutoCommit(false);
    37. Statement stat = conn.createStatement();
    38. ResultSet result = stat.executeQuery(sql);
    39. return result;
    40. } catch (SQLException e) {
    41. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
    42. } finally {
    43. }
    44. return null;
    45. }
    46. //插入
    47. public static void insert(String sql) {
    48. Connection conn = JdbcUtil.getConnection();
    49. Statement stat = null;
    50. try {
    51. stat = conn.createStatement();
    52. stat.executeUpdate(sql);
    53. } catch (SQLException e) {
    54. System.err.println( e.getClass().getName()+": "+ e.getMessage() );
    55. } finally {
    56. try {
    57. stat.close();
    58. conn.close();
    59. } catch (SQLException e) {
    60. e.printStackTrace();
    61. }
    62. }
    63. }
    64. }

    3.测试类

    1. import org.junit.Test;
    2. import java.sql.ResultSet;
    3. import java.sql.SQLException;
    4. /**
    5. * 类名称:
    6. *
    7. * @author 李庆伟
    8. * @date 2023年10月23日 10:07
    9. */
    10. public class DataTest {
    11. @Test
    12. public void firstTest() throws SQLException {
    13. String findSql = "SELECT test_id, test_name FROM test_one;";
    14. ResultSet rs = JdbcUtil.find(findSql);
    15. while (rs.next()) {
    16. String id = rs.getString("test_id");
    17. String name = rs.getString("test_name");
    18. System.out.println(id+"\t"+name);
    19. }
    20. }
    21. @Test
    22. public void insertTest() throws SQLException {
    23. //String findSql = "SELECT test_id, test_name FROM test_one;";
    24. String sql = "INSERT INTO test_one (test_id,test_name) "
    25. + "VALUES " + "('" + "5" + "', '" + "5" + "')";
    26. JdbcUtil.insert(sql);
    27. }
    28. }

    记录一点点。。。。。。。。。。。。

  • 相关阅读:
    Github创建远程仓库(项目)
    “智”造未来,江西同为科技(TOWE)参展第四届广州“两交会”圆满落幕
    抖音推荐算法的底层逻辑,视频没有走到爆款中间经历了什么?
    Chromium源码由浅入深(一)
    openGL之纹理
    动态组件<component>
    Python遍历列表时删除元素
    java计算机毕业设计新能源汽车租赁管理系统源程序+mysql+系统+lw文档+远程调试
    STM32F1与STM32CubeIDE编程实例-热敏传感器驱动
    Could not read from boot medium. System halted.
  • 原文地址:https://blog.csdn.net/liqingwei168/article/details/134037084