mYSQL中使用JDBC連接數據庫

1.前提準備:創建數據庫表

use day04_db;
create table user (
  id int primary key auto_increment,
  username varchar(20) not null,
  birthday date,
  sex char(1) default '男',
  address varchar(50)
);

insert into user values (null, '孫悟空','1980-10-24','男','花果山水簾洞');
insert into user values (null, '白骨精','1992-11-12','女','白虎嶺白骨洞');
insert into user values (null, '豬八戒','1983-05-20','男','福臨山雲棧洞');
insert into user values (null, '蜘蛛精','1995-03-22','女','盤絲洞');

select * from user;





2.知識點梳理

#需要導入mysql的驅動包
在這裏插入圖片描述
JDBC操作數據庫總結:

2.1.註冊驅動:

兩種方式:
new Driver();
Class.forName(“com.mysql.jdbc.Drver”);

2.2創建鏈接:

2.1使用Connection連接,Connection內部有三個參數
url=jdbc:mysql://連接的ip地址,本機使用local host或者127.0.0.1 username數據庫的用戶名 password 數據庫密碼
四大連接對象一般抽取爲properties文件
使用方法:Connection connection=DriverManager.getConnection();

2.3.Connection是一個接口,其有兩個實現類,Statement,prepareStatement

#二者區別:prepareStatement可以防止sql注入問題
#prepareStatement內部使用佔位符的方式
#prepareStatement使用預編譯
#3獲取發送sql語句的對象
#String sql=“操作數據庫表的語句”;
#ParpareStament parpareStament=connection.createParpareStament();

2.4.發送sql語句

#注意:增刪改使用executeUpdate,查詢使用execute Query();
#查詢有結果集需要處理
#Result result = parpareStament.executeUpdate(sql);

2.5.處理結果集

#處理結果集使用next()在循環中,循環一般使用while循環

2.6釋放資源:

result.close();
parpareStament.close();
connection.close();

3.案例代碼

在這裏插入圖片描述
url=jdbc:mysql://localhost/day04_db
username=root
password=root
driverClass=com.mysql.jdbc.Driver


package cn.itcast.test;

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


public class JDBCText {
    public static void main(String[] args) throws Exception {
        //使用JDBC連接數據庫總共分爲6步
        Properties properties = new Properties();
        InputStream resourceAsStream = JDBCText.class.getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(resourceAsStream);
        String driverClass = properties.getProperty("driverClass");
        String url = properties.getProperty("url");
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        //1.註冊驅動
        Class.forName(driverClass);

        //2.獲取鏈接

        Connection connection = DriverManager.getConnection(url, username, password);

        //3.生成發送SQL語句的對象
        String sql="select * from user";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //4.發送SQL語句
        ResultSet resultSet = preparedStatement.executeQuery();
        //5.處理結果集
        while (resultSet.next()) {
            String id = resultSet.getString("id");
            String username1 = resultSet.getString("username");
            Date birthday = resultSet.getDate("birthday");
            String sex = resultSet.getString("sex");
            String address = resultSet.getString("address");
            System.out.println(id+username1+birthday+sex+address);
        }
        //6.釋放資源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章