博客
关于我
jdbc操作数据库基础: 查询的例子
阅读量:265 次
发布时间:2019-03-01

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

package org.example.jdbc;import java.sql.*;public class FirstExample {    // 数据库信息    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";    static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=CTT";    // 用户名、密码    static final String USER = "root";    static final String PASS = "jianan";    public static void main(String[] args) {        Connection conn = null;        Statement stmt = null;        ResultSet rs = null;        try {            // 加载数据库驱动            Class.forName(JDBC_DRIVER);            // 连接数据库            conn = DriverManager.getConnection(DB_URL, USER, PASS);            // 执行sql            stmt = conn.createStatement();            String sql = "SELECT id, age, first, last FROM Employees";            rs = stmt.executeQuery(sql);            // 遍历结果集            while (rs.next()) {                int id = rs.getInt("id");                int age = rs.getInt("age");                String first = rs.getString("first");                String last = rs.getString("last");                System.out.printf("%d,%d,%s,%s", id, age, first, last);            }        } catch (Exception e) {            e.printStackTrace();        } finally {            // 释放数据库相关资源            try {                if (rs != null) {                    rs.close();                }            } catch (SQLException e) {                e.printStackTrace();            }            try {                if (stmt != null) {                    stmt.close();                }            } catch (SQLException e) {                e.printStackTrace();            }            try {                if (conn != null) {                    conn.close();                }            } catch (SQLException e) {                e.printStackTrace();            }        }    }}/*1,28,贾,楠2,29,孙,晨曦*/

 

转载地址:http://mwoa.baihongyu.com/

你可能感兴趣的文章
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
mysql union orderby 无效
查看>>
mysql where中如何判断不为空
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>