配置文件properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=123456

写一个类加载驱动,获取连接,释放连接资源连接配置文件

package com.esd.utils;

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

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

    static{
        try{

            InputStream in =JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");

            Properties properties=new Properties();

            properties.load(in);
//            properties.load(in);

            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 getConnection() throws SQLException {
        return DriverManager.getConnection(url,username,password);
    }
    //释放连接资源
    public static void release(Connection conn, Statement st, ResultSet rs){
        if(rs!=null){
            try{
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(st!=null){
            try{
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn!=null){
            try{
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

增加数据库数据

package com.esd;


import com.esd.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestUtils {
    public static void main(String[] args) {

        Connection conn= null;
        Statement st =null;
        ResultSet rs =null;

        try {

            conn = JdbcUtils.getConnection();


            st =conn.createStatement();
            String sql ="INSERT INTO users(`id`,`name`,`password`,`email`,`birthday`)"+
                    "VALUES (4,'lin','123456','1737191246@qq.com','2022-02-03')";

            long l = st.executeLargeUpdate(sql);
            if(l>0){
                System.out.println("插入成功!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.release(conn,st,rs);
        }
    }
}

删除其中某一行

package com.esd.utils;


import com.esd.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestUt {
    public static void main(String[] args) {

        Connection conn= null;
        Statement st =null;
        ResultSet rs =null;

        try {

            conn = JdbcUtils.getConnection();


            st =conn.createStatement();
            String sql ="DELETE FROM users WHERE id=4";

            long l = st.executeLargeUpdate(sql);
            if(l>0){
                System.out.println("删除成功!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.release(conn,st,rs);
        }
    }
}

package com.esd;



import com.esd.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Testutil {
    public static void main(String[] args) {

        Connection conn= null;
        Statement st =null;
        ResultSet rs =null;

        try {

            conn = JdbcUtils.getConnection();


            st =conn.createStatement();
            String sql ="UPDATE `users` SET `name`='huang' WHERE id=1";

            long l = st.executeLargeUpdate(sql);
            if(l>0){
                System.out.println("修改成功!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.release(conn,st,rs);
        }
    }
}

增删改只变动

String sql ="UPDATE `users` SET `name`='huang' WHERE id=1";这一句" "内容在SQLyog中先写好运行可以再复制

查询(executeQuery)

package com.esd.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestSelect {
    public static void main(String[] args) {
        Connection conn =null;
        Statement st=null;
        ResultSet rs=null;

        try{
            conn = JdbcUtils.getConnection();
            st=conn.createStatement();
//            SQL
            String sql= "select * from users where id=1 ";
            rs = st.executeQuery(sql);
            while (rs.next()){
                System.out.println(rs.getString("name"));

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

    }
}

 

posted on 2022-05-17 17:54  阿霖找BUG  阅读(16)  评论(0编辑  收藏  举报