JDBC练习四

本文最后更新于:2021年9月20日 晚上

JDBC练习四

import java.sql.*;
import java.util.*;
/*
	使用资源绑定器
*/
public class JDBCTest04 {
    public static void main(String[] args) {

        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");

        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(driver);

            conn = DriverManager.getConnection(url,user,password);

            stmt = conn.createStatement();

            int count = stmt.executeUpdate("insert into student values(5,'田七')");

            System.out.println(count == 1? "保存成功":"保存失败");

        } catch(SQLException e){
            e.printStackTrace();
        } catch(ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch(SQLException e){
                    e.printStackTrace();
                }
            }
            if(stmt != null) {
                try {
                    stmt.close();
                } catch(SQLException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

资源绑定器:

image-20210920184236414

资源绑定器内容为:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
user=root
password=123456

运行结果:

image-20210920190549218

image-20210920191031060


本文作者: CodeAnime
本文链接: https://codeanime.cc/JDBC%E7%BB%83%E4%B9%A0%E5%9B%9B.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!