idea查询、修改数据表

查询表

  1. 加载驱动 固定写法,加载驱动
  2. 用户信息和url
  3. 连接成功,数据库对象 connection 代表数据库
  4. 执行sql的对象 statement 执行sql的对象
  5. 执行sql的对象去执行sql,可能存在结果,查看返回结果
  6. 关闭资源,释放连接
package JDBCmysql.Demo01;

import java.sql.*;

/**连接数据库查询
 * @author liu
 */
public class Demo01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动   固定写法,加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2。用户信息和url
        String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=ture&characterEncoding=utf8&useSSL=true";
        String userName = "root";
        String password = "123456";

        //3。连接成功,数据库对象   connection 代表数据库
        Connection connection = DriverManager.getConnection(url, userName, password);

        //4。执行sql的对象 statement 执行sql的对象
        Statement statement = connection.createStatement();

        //5。执行sql的对象去执行sql,可能存在结果,查看返回结果
        String sql = "select * from users";
        ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,里面封装了我们全部的查询结果
        while(resultSet.next()) {
            System.out.println("id=" + resultSet.getObject("id"));
            System.out.println("name=" + resultSet.getObject("name"));
            System.out.println("psw=" + resultSet.getObject("psw"));
            System.out.println("email=" + resultSet.getObject("email"));
            System.out.println("bir=" + resultSet.getObject("birthday"));
            System.out.println("--------------------------------------");
        }
        //6。关闭资源,释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}

修改表

  • 工具类
package JDBCmysql.Demo02;


import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class Demo01 {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static {
        try {
            //获取文本放在流里面
            InputStream is = Demo01.class.getClassLoader().getResourceAsStream("jdbcLogin");
            //创建Properties加载流的内容
            Properties properties = new Properties();
            properties.load(is);
            //获取需要的目标内容
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("userName");
            password = properties.getProperty("password");

            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getTConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    //。关闭资源,释放连接
    public static void Cclose(ResultSet rs,Statement statement, Connection connection) {
        if (rs != null) {
            try {
                rs.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();
            }
        }
    }
}
  • 文本
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=ture&characterEncoding=utf8&useSSL=true
userName = root
password = 123456
  • main线程
package JDBCmysql.Demo02;


import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class Demo01 {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static {
        try {
            //获取文本放在流里面
            InputStream is = Demo01.class.getClassLoader().getResourceAsStream("jdbcLogin");
            //创建Properties加载流的内容
            Properties properties = new Properties();
            properties.load(is);
            //获取需要的目标内容
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("userName");
            password = properties.getProperty("password");

            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getTConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    //。关闭资源,释放连接
    public static void Cclose(ResultSet rs,Statement statement, Connection connection) {
        if (rs != null) {
            try {
                rs.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();
            }
        }
    }
}
posted @   小幼虫虫  阅读(614)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示