天天看點

JDBC的增删改查JDBC的增删改查

JDBC的增删改查

今天在講師的耐心講解下,我們學習了用IDEA軟體進行JDBC的增删改查操作。

老師利用投屏軟體先示範了JDBC的七步驟:

1.加載驅動

2.建立連結

3.寫資料庫

4. 得到statement對象

5. 執行資料庫,得到結果集

6. 處理結果集

7.關閉資源

我們在老師的引導下嘗試用IDEA進行了程式設計。

擷取資料庫的連接配接

public static Connection getConnection() {//建立連接配接
        Connection connection = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc1?useSSL=true&characterEncoding=utf-8&user=root&password=whuthhy");
            //connection=DriverManager.getConnection(url,username,password);

            return connection;

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

        return null;
    }
           

關于url的寫法,url=“jdbc:mysql://127.0.0.1:3306/(+目标資料庫名字+)?useSSL=true&characterEncoding=utf-8&user=(你的MySQL使用者名)&password=(你的密碼)”

資源關閉

public static void Close(PreparedStatement statement, Connection connection) {
//資源關閉

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
           

之是以将這兩部分列出來是因為增删改查都要用到這兩部分,可以把他們放到DBUtil包裡,需要就調用,進行代碼簡化。

我寫的增删改查語句

目标資料庫jdbc1

JDBC的增删改查JDBC的增删改查
JDBC的增删改查JDBC的增删改查

查詢

String sql="select * from yonghu";
                //4.得到statement對象
                statement = DBUtil.getConnection().prepareStatement(sql);
                //5.執行sql  得到結果集
                resultSet = statement.executeQuery();
                //6.處理結果集
                while(resultSet.next()){
                    System.out.println(resultSet.getInt(1));
                    System.out.println(resultSet.getString(2));
                    System.out.println(resultSet.getString(3));
                }
           

運作截圖

JDBC的增删改查JDBC的增删改查

增加/插入

String sql = "insert into yonghu (username,password) values (?,?)";

            statement = DBUtil.getConnection().prepareStatement(sql);
            statement.setString(1, "二狗子");
            statement.setString(2, "www");
 int res = statement.executeUpdate();
            if(res>0){
                System.out.println("增加資料成功!!!");
            }
           

運作截圖

JDBC的增删改查JDBC的增删改查
JDBC的增删改查JDBC的增删改查

多次增加資料

JDBC的增删改查JDBC的增删改查

删除

String sql="delete from yonghu where id>6";

            statement = DBUtil.getConnection().prepareStatement(sql);
            //statement.executeUpdate();
            //executeUpdate 的傳回值是一個整數,訓示受影響的行數(即更新計數)。對于 CREATE TABLE 或 DROP TABLE 等不操作行的語句,executeUpdate 的傳回值總為零。
            int res = statement.executeUpdate();
            if(res>0){
                System.out.println("刪除資料成功!!!");
            }
           

運作截圖

JDBC的增删改查JDBC的增删改查
JDBC的增删改查JDBC的增删改查

更新

String sql="update yonghu set username = ?, password = ? where id = ?";


            statement = DBUtil.getConnection().prepareStatement(sql);
            ((PreparedStatement) statement).setString(1,"tom");
            ((PreparedStatement) statement).setString(2,"666666");
            ((PreparedStatement) statement).setString(3,"1");
            //((PreparedStatement) statement).executeUpdate();
            int res = ((PreparedStatement) statement).executeUpdate();
            if(res>0){
                System.out.println("更新資料成功!!!");
            }
           

運作截圖

JDBC的增删改查JDBC的增删改查
JDBC的增删改查JDBC的增删改查

個人體會

本次主要是熟悉JDBC和IDEA的使用,IDEA的确很友善。還要注意的就是檔案分層,還有代碼簡化,我還有很多地方要去改進。