zoukankan      html  css  js  c++  java
  • Day3-JAVA—JDBC

    Demo3.java

    import java.sql.*;
    
    /**
     * jdbc 第一个示例,读取数据
     */
    public class Demo3 {
    
        public static void main(String[] args) {
    
            String url = "jdbc:mysql://127.0.0.1:3306/course";
            String userName = "root";
            String password = "123456";
            String sql = "select id ,name ,age from tb_user;";
    
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
    
            try {
                // 1.建立连接
                connection = DriverManager.getConnection(url, userName, password);
    
                // 2.具体操作
                // 2.1 预编译 SQL
                statement = connection.prepareStatement(sql);
                // 2.2 执行查询
                resultSet = statement.executeQuery();
                // 2.3 处理结果集
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    // 不建议使用
                    // int id2 = resultSet.getInt(1);
                    // System.out.println("id2="+id2);
                    String name = resultSet.getString("name");
                    int age = resultSet.getInt("age");
                    System.out.println("id = " + id + ", name=" + name + ", age=" + age);
                }
    
            } catch (SQLException e) {
                e.printStackTrace();// 不推荐的异常处理方式
            } finally {
                // 3.资源关闭
                if (resultSet != null) {
                    try {
                        resultSet.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
    
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
    
                }
    
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    Demo0002.java (自写)

    package course.basic.jdbc;
    
    import java.sql.*;
    
    public class Demo0002 {
        public static void main(String[] args) throws SQLException {
            String url = "jdbc:mysql://127.0.0.1:3306/course?serverTimezone=UTC";
            String userName = "root";
            String passWord = "123456";
            String sql = "select * from tb_user";
    
    
            //1、与数据库建立连接
            Connection connection = DriverManager.getConnection(url, userName, passWord);
    
    //        2、具体操作
    //          2.1 预编译SQL
            PreparedStatement statement = connection.prepareStatement(sql);
    //          2.2 执行sql    查询
            ResultSet resultSet = statement.executeQuery();
    
    //         2.3处理结果集
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
    
                System.out.println("id = "+id+ ",name=" +name+ ",age=" +age);
            }
            
    //        3.资源关闭
            resultSet.close();
            statement.close();
            connection.close();
        }
    }
  • 相关阅读:
    偏最小二乘法回归(Partial Least Squares Regression)
    今天就来聊聊产品运营
    VS2005终于不“变态”了!
    Android 里的对话框Dialog 实现机制基础
    C#多线程操作界面控件的解决方案
    转C++ ,C#数据类型对照
    关于Linq to sql 应用时出现的一个‘row not found or changed’ 异常
    Android之Context Memu
    HttpModule的认识
    Docker:官网文档 Get Started 笔记
  • 原文地址:https://www.cnblogs.com/flynn0825/p/12934940.html
Copyright © 2011-2022 走看看