目錄
1.什么是JDBC編程?
JDBC是指 Java數(shù)據(jù)庫的連接,是一種標(biāo)準(zhǔn)Java應(yīng)用編程接口(JAVA API),用來連接Java編程語言和廣泛的數(shù)據(jù)庫
2.MySQL驅(qū)動包安裝
2.1.mysql驅(qū)動包
JDBC編程需要用到mysql的驅(qū)動包(驅(qū)動包就是把mysql自身的api個轉(zhuǎn)換成JDBC風(fēng)格的)
2.2.驅(qū)動包及其下載
驅(qū)動包是由各個數(shù)據(jù)庫官方提供的,本文用的的mysql驅(qū)動包
2.3安裝具體流程
我們采取的是從maven中央倉庫下載Maven : // ()
下載完之后就會有一個jar包,這個就和電腦上的zip壓縮文件一樣
到這一步mysql的驅(qū)動包就已經(jīng)完成了!!!
3.引入依賴
3.1.創(chuàng)建項目
這個就不用我多說了吧,就是正常的
3.2.導(dǎo)入依賴 1)隨便創(chuàng)建一個目錄test
2)復(fù)制剛剛的jar包
3)加入庫add as
4)完成!!!
4.JDBC代碼的的基本流程(編寫) 1)創(chuàng)建 對象,這個對象描述了數(shù)據(jù)庫服務(wù)器在哪
DataSource dataSource = new MysqlDataSource();
2)詳細(xì)描述數(shù)據(jù)庫服務(wù)器在哪
//設(shè)置數(shù)據(jù)庫所在的地址
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false"); // 向下轉(zhuǎn)型
//設(shè)置登錄數(shù)據(jù)庫的用戶名
((MysqlDataSource)dataSource).setUser("root");
//設(shè)置登錄數(shù)據(jù)庫的密碼
((MysqlDataSource)dataSource).setPassword("wangbin");
詳細(xì)了解!!!
MySQLURL:jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false
1. jdbc:mysql:固定寫法
2. 127.0.0.1:mysql服務(wù)器所在的主機(jī)ip,127.0.0.1是一個特殊的寫法,表示
3. 3306:端口號,表示mysql服務(wù)器的位置
4.=utf8:表示字符集為utf8形式
5. = false表示不需要加密,true就表示加密,由于電腦并沒有什么貴重的資源,也就不需要進(jìn)行加密
3)數(shù)據(jù)庫的連接
Connection connection = dataSource.getConnection();
//數(shù)據(jù)庫的連接
4)用戶的輸入
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入id");
int id = scanner.nextInt();
//System.out.println("請輸入name");
//String name = scanner.next();
5)拼裝sql語句(此處演示)
String sql = "delete from student where id = ?";
6)講過sql包裝成一個語句對象
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);//替換上述索引
System.out.println(statement);//相當(dāng)于一個日志
7)執(zhí)行sql
int ret = statement.executeUpdate();
System.out.println(ret);
8)資源的釋放
statement.close();
connection.close();
資源的釋放類似于棧的原理,后使用的資源先釋放,先使用的資源后釋放
資源的釋放可以將占用的資源釋放回去,供其他人使用
以上八個操作基本 就已經(jīng)寫完一個基本的JDBC編程!!!下面讓我來看完整的代碼
5.JDBC的增刪改 5. 插入
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
//用戶的輸入 id name
public class TestJDBC {
public static void main(String[] args) throws SQLException {

//創(chuàng)建好數(shù)據(jù)源
DataSource dataSource = new MysqlDataSource();
//設(shè)置數(shù)據(jù)庫所在的地址
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false"); // 向下轉(zhuǎn)型
//設(shè)置登錄數(shù)據(jù)庫的用戶名
((MysqlDataSource)dataSource).setUser("root");
//設(shè)置登錄數(shù)據(jù)庫的密碼
((MysqlDataSource)dataSource).setPassword("wangbin");
// 2.創(chuàng)建連接
Connection connection = dataSource.getConnection();
//用戶的輸入
Scanner scanner = new Scanner(System.in);
// 拼裝sql語句
String sql = "insert into student values(?,?),(?,?),(?,?)";
//將SQL包裝成一個語句對象
PreparedStatement statement = connection.prepareStatement(sql);
//進(jìn)行替換操作
int j = 1;
for (int a = 0; a < 3; a++) {
System.out.println("請輸入id");
int id = scanner.nextInt();
System.out.println("請輸入name");
String name = scanner.next();
statement.setInt( j++, id);
statement.setString(j++, name);
}
System.out.println("statement " + statement); // 簡單的日志
//執(zhí)行sql
int ret = statement.executeUpdate();
System.out.println(ret);
//釋放資源
statement.close();
connection.close();

}
}
5.2.刪除
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
//刪除id為1的學(xué)生
public class TestJDBCdelete {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("wangbin");
Connection connection = dataSource.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入id");
int id = scanner.nextInt();
String sql = "delete from student where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
System.out.println(statement);
int ret = statement.executeUpdate();
System.out.println(ret);
statement.close();
connection.close();
}
}
5.3.修改
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
//根據(jù)id修改學(xué)生姓名
public class TeatJDBCupdate {
public static void main(String[] args) throws SQLException {
DataSource dataSource =new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("wangbin");
Connection connection = dataSource.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入學(xué)生id");
int id = scanner.nextInt();
System.out.println("請輸入學(xué)生姓名");
String name = scanner.next();
String sql = "update student set name = ? where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);//表示一個語句
statement.setInt(2,id);
statement.setString(1,name);
System.out.println("statement " + statement); //簡單的日志
int ret = statement.executeUpdate();
System.out.println(ret);
statement.close();
connection.close();
}
}
6.JDBC的
稍微和增刪改有點不同
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestJDBCSelect {
public static void main(String[] args) throws SQLException {
//創(chuàng)建數(shù)據(jù)源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("wangbin");
//建立連接
Connection connection = dataSource.getConnection();
//拼裝SQL
String sql = "select * from student";
PreparedStatement statement = connection.prepareStatement(sql);
//執(zhí)行SQL
ResultSet resultSet = statement.executeQuery();
//遍歷結(jié)果結(jié)合,類似迭代器
//next 方法獲取到一行記錄,同時吧光標(biāo)向后移動一行
while(resultSet.next()){
//針對當(dāng)前這一行獲取到每一列的信息
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println(id + name);
}
resultSet.close();
statement.close();
connection.close();
}
}
操作需要用到.next()這個方法,類似于迭代器,獲取到每一行的信息,資源的釋放也是增加了一個
7.總結(jié)
對于初學(xué)者來說JDBC可能會比較的抽象,但是寫了幾遍后就會知道基本是一個模板sql2008數(shù)據(jù)庫驅(qū)動包,熟能生巧sql2008數(shù)據(jù)庫驅(qū)動包,加油