idea 2020.1 连接MySQL数据库的两种方法

方法一:

结构:

MySQL数据库和表

表结构:

注:id为自增

数据库配置文件:

drive=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/
database=student
user=root
password=123456

 连接数据库jar包

 

把数据库的连接和关闭抽成一个工具类,这样方便后续的工作:

package com.test;

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


public class JDBCUtil {
    public static String drive;
    public static String url;
    public static String database;
    public static String user;
    public static String password;
    private static Connection cn;



    static {
        Properties properties=new Properties();
        try {

            properties.load(JDBCUtil.class.getResourceAsStream("jdbc.properties"));

            drive = properties.getProperty("drive");
            url = properties.getProperty("url");
            database = properties.getProperty("database");
            user = properties.getProperty("user");
            password = properties.getProperty("password");



        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    //连接测试
    public static Connection connetion(){

        try {
            //注册驱动
            Class.forName(drive);
            //连接数据库
            cn= DriverManager.getConnection
                    (url+database,user,password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return cn;

    }

    //释放资源
    public static void Rease(Connection cn, Statement st, ResultSet rt){
        clossCT(cn);
        clossSM(st);
        clossRS(rt);

    }

    //关闭connection连接
    public  static void clossCT(Connection connection){
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                connection=null;
            }

        }
    }
    //关闭Statement连接
    public  static void clossSM(Statement st){
        if (st!=null){
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                st=null;
            }

        }
    }
    //关闭ResultSet连接
    public  static void clossRS(ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                rs=null;
            }

        }
    }
}

对数据库的增删改查弄成一个接口:

package imp;

import student.Stu;

import java.util.List;

public interface update {
    /**
     *查询所有
     */
    public List<Stu> allStu();
    /**
     * 查询单个
     */
    public Stu singleStu(in
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值