JavaWeb–JDBC实验

  • Post author:
  • Post category:java



目录


前言:


JDBC代码:


db.properties:连接配置


DButil:功能


DBTestInsert:插入功能


DBTestDelete:删除功能


DBTestUpdate:修改功能


DBTestSelect:查询功能


小结:


前言:

本实验主要针对于在自己的JDBC实验中遇到的问题和整个实验中的注意事项和内容整理。

JDBC代码:

db.properties:连接配置

driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=true
username = root
password = Acad112625!

DButil:功能

package com.xjdx.utils;

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

import static java.lang.System.in;

public class DButil {

    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static{

        try {
            InputStream in = DButil.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(in);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            //1.驱动只用加载一次
            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 e) {
                e.printStackTrace();
            }
        }
        if (st != null){
            try {
                st.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
        }
        if (conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}

DBTestInsert:插入功能

package com.xjdx.utils;

import com.xjdx.utils.DButil;

import java.sql.*;

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

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

        try {
            conn = DButil.getConnection();//获取数据库连接
            st = conn.createStatement();//获得SQL的执行对象
            String sql = "insert into users(id, name, password, email, birthday) values (5, 'Niko','123456','NIko@outlook.com','1997-12-21')";
            int i = st.executeUpdate(sql);
            if (i > 0){
                System.out.println("Insert successfully");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DButil.release(conn,st,rs);
        }

    }
}

DBTestDelete:删除功能

package com.xjdx.utils;

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

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

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

        try {
            conn = DButil.getConnection();//获取数据库连接
            st = conn.createStatement();//获得SQL的执行对象
            String sql = "delete  from  users where  id=2;";//删除SQL语句
            int i = st.executeUpdate(sql);

            if (i > 0){
                System.out.println("Delete successfully");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DButil.release(conn,st,rs);
        }
    }
}

DBTestUpdate:修改功能

package com.xjdx.utils;

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

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

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

        try {
            conn = DButil.getConnection();//获取数据库连接
            st = conn.createStatement();//获得SQL的执行对象
            String sql = "update users set name = 'electronic' , email = 'electronic@outlook.com' where  id = 3";//UpdateSQL语句
            int i = st.executeUpdate(sql);

            if (i > 0){
                System.out.println("Update successfully");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DButil.release(conn,st,rs);
        }
    }
}

DBTestSelect:查询功能

package com.xjdx.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.xjdx.utils.DButil;

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

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

        try {
             conn = DButil.getConnection();
             st = conn.createStatement();
             //SQL
            String sql = "select * from users";
            rs = st.executeQuery(sql);//查询完毕会返回一个结果集
            while(rs.next()){
                System.out.print(rs.getInt("id"));
                System.out.print(", "+rs.getString("name"));
                System.out.print(", "+rs.getString("password"));
                System.out.print(", "+rs.getString("email"));
                System.out.println(", "+rs.getString("birthday"));
            }
            System.out.println("Select successfully");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DButil.release(conn,st,rs);
        }
    }
}

小结:

1.数据库的连接注意事项

MySQL 8.0 以下版本 – JDBC 驱动名及数据库 URL

static final String JDBC_DRIVER = “com.mysql.jdbc.Driver”;

static final String DB_URL = “jdbc:mysql://localhost:3306/数据库名称”;

MySQL 8.0 以上版本 – JDBC 驱动名及数据库 URL

static final String JDBC_DRIVER = “com.mysql.cj.jdbc.Driver”;

static final String DB_URL=”jdbc:mysql://localhost:3306/数据库名称?useSSL=false &allowPublicKeyRetrieval=true&serverTimezone=UTC”;

2.增删改和查:executeUpdate方法用于向数据库发送增、删、改的SQL语句;executeQuery方法用于向数据库发送查询语句,executeQuery方法返回的是代表查询结果的ResultSet对象,executeQuery()查询完毕会返回一个结果集。

3.使用完数据库后一定要释放资源。

4.担心中文乱码的可以在url的?后加上useUnicode=true&characterEncoding=utf-8&useSSL=true。



版权声明:本文为RainsKianna原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。