java最原始连接数据库方法代码

[复制链接]
admin 发表于 2025-8-18 13:43:46 | 显示全部楼层 |阅读模式
java最原始连接数据库方法代码
  1. package com.jinhei.base;

  2. import java.sql.*;

  3. public class JDBCQuick {
  4.     public static void main(String[] args) throws Exception {
  5.         // 1,注册驱动
  6.         // Class.forName("com.mysql.cj.jdbc.Driver");
  7.         //2.获取数据库连接
  8.         Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jinhei", "root","root");
  9.         //3.创建Statement对象
  10.         Statement statement = connection.createStatement();
  11.         //4.编写SQL语句并执行,获取结果
  12.         String sql = "select * from t_emp";
  13.         ResultSet resultSet = statement.executeQuery(sql);
  14.         //5.处理结果
  15.         while (resultSet.next()) {
  16.             int empId = resultSet.getInt("emp_id");
  17.             String empName = resultSet.getString("emp_name");
  18.             String empSalary = resultSet.getString("emp_salary");
  19.             int empAge = resultSet.getInt("emp_age");
  20.             System.out.println(empId + "\t" + empName + "\t" + empSalary + "\t" + empAge);
  21.         }

  22.         //6.释放资源(先开后关原则)
  23.         resultSet.close();
  24.         statement.close();
  25.         connection.close();

  26.     }
  27. }
复制代码
网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:17817817816
微信扫码

网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等

粤ICP备2024252464号

在本版发帖
微信扫码
QQ客服返回顶部
快速回复 返回顶部 返回列表