JDBC編程實例分析及連接數據庫錯誤總結

JDBC編程實例分析及連接數據庫錯誤總結

實例

數據庫中初始的表格
在這裏插入圖片描述

1.把getConnection寫成一個方法,完成加載驅動和獲取數據庫連接,後續直接調用

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//把驅動加載和數據庫連接直接寫成一個方法,後續一直調用
public class GetConnection {
    public static final String URL = "jdbc:mysql://localhost:3306/jdbc_test?characterEncoding=UTF-8";
    public static final String USER = "root";
    public static final String PWD = "root";

    public static Connection getConnection(){
        Connection connection = null;
        try{
            //加載驅動
            Class.forName("com.mysql.jdbc.Driver");

            //獲取連接
            connection = DriverManager.getConnection(URL,USER,PWD);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

}

2.實例

import com.sun.deploy.xml.GeneralEntity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo {
    public static Connection connection = null;
    public static PreparedStatement preparedStatement = null;
    public static ResultSet resultSet = null;

    public static void main(String[] args) {
        //可執行所有sql語句
        execute();
        //執行增刪改語句
        //executeUpdate();
        //執行查詢語句,並且顯示結果
        //executeQuery();

    }

    //增刪改數據
    public static void executeUpdate() {
        //在sql中,後面要用preparedStatement.set  ()設置的直接用?號佔位
        String sql = "INSERT INTO student (id,`name`,password,grade) VALUES (?,?,?,?)";
        try {
            connection = GetConnection.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, 2);
            preparedStatement.setString(2, "Rose");
            preparedStatement.setString(3, "12345678");
            preparedStatement.setString(4, "2019");
            int count = preparedStatement.executeUpdate();
            System.out.println("插入" + count + "條數據");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
//            關閉釋放資源(從裏到外,從後到前)
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //查詢數據
    public static void executeQuery(){
            String sql = "select id,`name`,password,grade from student where grade=?";
            try {
                connection = GetConnection.getConnection();
                preparedStatement = connection.prepareStatement(sql);
                //注意:注意數據類型,string要加引號
                preparedStatement.setString(1, "2019");
                resultSet = preparedStatement.executeQuery();
                List<Student> studentList = new ArrayList<>();
                while (resultSet.next()) {
                    Student student = new Student();
                    student.setId(resultSet.getInt("id"));
                    student.setName(resultSet.getString("name"));
                    student.setPassword(resultSet.getString("password"));
                    student.setGrade(resultSet.getString("grade"));
                    System.out.println(student.getName());
                    studentList.add(student);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (resultSet != null) {
                    try {
                        resultSet.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (preparedStatement != null) {
                    try {
                        preparedStatement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
    }

    //使用execute()執行增刪改查
    public static void execute() {
        //查詢
        String sql = "SELECT id,`name`,grade FROM student WHERE id=?";

        //更新
        //String sql = "UPDATE student SET grade=? WHERE name=?";
        try {
            connection = GetConnection.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            //查詢
            preparedStatement.setInt(1,2);

            //更新
            //preparedStatement.setString(1, "2020");
            //preparedStatement.setString(2, "Rose");
            boolean resultSet1 = preparedStatement.execute();
            if (resultSet1) {
                resultSet = preparedStatement.getResultSet();
                List<Student> studentList = new ArrayList<>();
                while (resultSet.next()) {
                    Student student = new Student();
                    student.setId(resultSet.getInt("id"));
                    student.setName(resultSet.getString("name"));
//                    student.setPassword(resultSet.getString("password"));
                    student.setGrade(resultSet.getString("grade"));
                    System.out.println(student.getName());
                    studentList.add(student);
                }
            } else {
                int count = preparedStatement.executeUpdate();
                System.out.println("插入" + count + "條數據");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

錯誤總結

錯誤:沒有配置database
打開database
view——>Tool Windows——>Database

  • 點框內圖標
    在這裏插入圖片描述
  • 進入data sources and drivers.點左上+號添加mysql,然後設置要填寫的用戶名user,密碼password,數據庫名稱database,然後點擊test connection測試連接

在這裏插入圖片描述

  • 出現錯誤:找不到驅動文件,點擊錯誤下面的change driver class
  • 下面的警告處,點擊download進行下載時,可能下載不下來,可以手動下載
  • 手動下載連接的jar包
    https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.15/mysql-connector-java-8.0.15.jar’:

在這裏插入圖片描述

  • change driver class 進入如下頁面,點擊加號,選擇provided driver,選擇mysql connection,再選擇版本進行下載即可。然後apply

在這裏插入圖片描述

  • 出現錯誤:沒有設置時區

在這裏插入圖片描述

  • 進入Advanced,找到serverTimezone,設置其值爲UTC

在這裏插入圖片描述

  • 時區問題解決了,但是還需要在Advanced裏面,把useSSL的值設置爲TURE。否則運行時可能出現錯誤

Sun Aug 05 21:18:18 CST 2018 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

在這裏插入圖片描述

  • 另外進入Schemas選擇All schemas,然後apply

在這裏插入圖片描述

  • 最後回到General,再次測試連接test connection,再OK即可
    在這裏插入圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章