今日报告

package com.test.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBConnection {
    //加载jdbc连解MySQL;
    public final static String driver = "com.mysql.cj.jdbc.Driver";
    //连解数据库地址    
    public final static String url = "jdbc:mysql://localhost:3306/runoob ?serverTimezone=UTC";//runoob代表库的名称
    //连解MySQL的用户名
    public final static String user="root";
    //密码
    public final static String pwd="301101zw.";
    
    //static静态代码块加载jdbc的驱动
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        
    }
    //连接mysql的连接对象
    public static Connection getConn() {
        try {
            return DriverManager.getConnection(url,user,pwd);
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return null;
    }
    //关闭连接,保证mysql资源的释放,能够充分使用资源
    public static void close(ResultSet rs,PreparedStatement ps,Connection conn) {
        try {
            if(rs!=null) {
                rs.close();
            }
            if(ps!=null) {
                rs.close();
            }
            if(conn!=null) {
                rs.close();
            }
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    //验证jdbc的使用
    public static void main(String[] args) {
        System.out.println(getConn());//
    }
    
}
DBConnection

 

今天发现了我一直做不出的原因,eclipse没有调配好,以至于没办法做出。在哔哩哔哩的“村长教编程”中重新配置了东西,然后重新连接了jdbc数据库。
posted @ 2023-02-28 20:57  北·岛  阅读(9)  评论(0)    收藏  举报