jdbc连接MySQL数据库(完整文件+详细说明)

使用jdbc连接数据库:
可以直接在方法中定义url、user、psd等信息,也可以读取配置文件,但是在web项目中肯定是要使用第二种方式的,为了统一,只介绍第二种方式。
步骤
1、创建配置文件db.properties
无论是eclipse还是myeclipse,在工程下右键->new->file,以properties为后缀名就好了。
配置文件内容:

#连接数据库的url,如果主机地址是localhost,端口是3306也可以写成url=jdbc:mysql:///databasename
    url=jdbc:mysql://localhost:3306/databasename
    #用户名
    user=root
    #密码
    password=root
    #MySQL数据库加载驱动
    driverClass=com.mysql.jdbc.Driver

2、定义一个使用jdbc连接数据库的工具类JdbcUtil.java
工具类内容:

public class JdbcUtil{
    //定义全局变量
    private static String url = null;
    private static String user = null;
    private static String password = null;
    private static driverClass = null;
    //读取配置文件内容,放在静态代码块中就行,因为只需要加载一次就可以了
    static{
        try{
            Properties props = new Properties();
            //使用类路径加载的方式读取配置文件
            //读取的文件路径要以“/”开头,因为如果使用“.”的话,当部署到服务器上之后就找不到文件了,使用“/”开头会直接定位到工程的src路径下
            InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties");
            //加载配置文件
            props.load(in);
            //读取配置文件信息
            url = props.getProperty("url");
            user = props.getProperty("user");
            password = props.getProperty("password");
            driverClass = props.getProperty("driverClass");
            //注册驱动程序
            Class.forName(driverClass);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("驱动程序注册失败!!!");
        }
    }

    //获取连接对象Connection
    public static Connection getConnection(){
        try{
            return DriverManager.getConnection(url,user,password);
        }catch(SQLException e){
            e.printStackTrace();
            //跑出运行时异常
            throw new RuntimeException();
        }
    }

    //关闭连接的方法,后打开的先关闭
    public static void close(Connection conn,Statement stmt,ResultSet rs){
        //关闭ResultSet对象
        if(rs != null){
            try{
                //关闭rs,设置rs=null,因为java会优先回收值为null的变量
                rs.close();
                rs = null;
            }catch(SQLException e){
                e.printStackTrace();
                throw new RuntimeException();
            }
        }
        //关闭Statement对象,因为PrepareStatement和CallableStatement都是Statement的子接口,所以这里只需要有关闭Statement对象的方法就可以了
        if(stmt != null){
            try{
                stmt.close();
                stmt = null;
            }catch(SQLException e){
                e.printStackTrace();
                throw new RuntimeException();
            }
        }
        //关闭Connection对象
        if(conn != null){
            try{
                conn.close();
                conn = null;
            }catch(SQLException e){
                e.printStackTrace();
                throw new RuntimeException();
            }
        }
    }
}

有任何问题可以+QQ:3393055725
可以聊任何java问题,JavaSE、JavaEE

工具类已经实现了,可以直接考到项目里使用,但是有一点要注意,就是这个类文件中没有导入支持的类,大家也可以看到在类的头部没有package 和import,这个需要自己手动添加上,导入类的快捷键是Ctrl+Shift+O,导包的时候不要导错了;别忘了引入MySQL的支持jar包mysql-connector-java-5.1.7-bin.jar

  • 6
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
连接 MySQL 数据库需要使用 JDBC 驱动程序,下面是连接 MySQL 数据库详细步骤: 1. 下载并安装 MySQL JDBC 驱动程序 首先需要下载并安装 MySQL JDBC 驱动程序。你可以从 MySQL 官方网站上下载最新版本的驱动程序。 2. 导入 JDBC 驱动程序 将下载的 JDBC 驱动程序导入到你的项目中。如果你使用的是 Maven,则可以将以下依赖项添加到你的 pom.xml 文件中: ``` <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> ``` 如果你没有使用 Maven,那么可以将驱动程序的 JAR 文件添加到你的项目中。 3. 创建 JDBC 连接 使用以下代码创建 JDBC 连接: ``` import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JdbcConnection { public static void main(String[] args) { Connection connection = null; try { // 加载 MySQL JDBC 驱动程序 Class.forName("com.mysql.cj.jdbc.Driver"); // 创建 JDBC 连接 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password"); // 在此处添加与数据库交互的代码 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上面的代码中,我们使用 `DriverManager.getConnection()` 方法创建 JDBC 连接。在这个方法中,第一个参数是 JDBC URL,第二个参数是数据库用户名,第三个参数是数据库密码。 4. 关闭 JDBC 连接 在你完成与数据库的交互后,使用 `connection.close()` 方法关闭 JDBC 连接,以释放资源。 以上就是连接 MySQL 数据库详细步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值