zoukankan      html  css  js  c++  java
  • JDBC编程步骤

    加载驱动程序:

    Class.forName(driverClass)
    //加载MySql驱动
    Class.forName("com.mysql.jdbc.Driver")
    //加载Oracle驱动
    Class.forName("oracle.jdbc.driver.OracleDriver")

    获得数据库连接:

    String url = "jdbc:mysql://localhost:3306/student";
    String user = "root";
    String password = "123456";
    Connection connection = DriverManager.getConnection(url, user, password);

    创建Statement对象:

    Statement statement = connection.createStatement();

    例子:

    package com.silentteller.test;
    
    import java.sql.*;
    
    public class Test {
        public static void main(String[] args) {
            try {
                //1.加载驱动程序
                Class.forName("com.mysql.cj.jdbc.Driver");
                //2. 获得数据库连接
                String url = "jdbc:mysql://localhost:3306/student";
                String user = "root";
                String password = "123456";
                Connection connection = DriverManager.getConnection(url, user, password);
                System.out.println(connection);
                //3.操作数据库,实现增删改查
                Statement statement = connection.createStatement();
                String sql = "select * from student";
                ResultSet rs = statement.executeQuery(sql);
                //如果有数据,rs.next()返回true
                while(rs.next()){
                    System.out.println(rs.getString("id") + rs.getString("student_id") + rs.getString("name")
                                    + rs.getInt("age") + rs.getString("sex") + rs.getString("birthday"));
                }
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    转 自学编程这几点非常重要
    转 错误:ORA-28002/ORA-65162 : the password will expire within 7 days 解决方法
    Python基础3
    Python基础2
    Python基础1
    爬虫实战项目集合
    Python3中PyMongo的用法
    难点--均方误差(MSE)和均方根误差(RMSE)和平均绝对误差(MAE)
    Excel合并
    mongoDB常用操作命令
  • 原文地址:https://www.cnblogs.com/silentteller/p/12299727.html
Copyright © 2011-2022 走看看