数据库的连接

  1. 连接数据库时要导包,可以去jquery官网去找
    在这里插入图片描述
    2.例子模板:
package TestJava.uitl;

import java.sql.*;

public class DateBaseConnection {
    //地址
    static String url;
    //用户名
    static String user;
    //密码
    static String password;
    //链接方法
    public static Connection getConnection() throws ClassNotFoundException {
    /*地址的写法,jdbc是驱动:com.mysql.jdbc.Driver
  URL:jdbc:mysql://127.0.0.1:port/dbname
  注:127.0.0.1:数据库所在机器的名称
    port:端口号,默认是3306
    dbname:数据库名称
    ?后面的是协议协议限制,中文呀*/
        url = "jdbc:mysql://localhost:3308/issa?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT";
        // 用户名
        user = "rtt";
        // 密码
        password = "1236";
        Connection connection=null;
        Class.forName("com.mysql.cj.jdbc.Driver");
        try {
            connection= DriverManager.getConnection(url, user, password);
            System.out.println("successful connection");
        } catch (SQLException e) {
            System.out.println("fail to connect");
            throw new RuntimeException(e);
        }
        return connection;
    }
    //关闭链接
    public static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
           if(resultSet!=null){
               resultSet.close();
           }
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
    }
}

3.数据库连接很简单,但是我们要熟悉sql语句才能用好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

i s s a

感谢大佬支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值