目录
1)创建Datasource 对象,这个对象描述了数据库服务器在哪
1.什么是JDBC编程?
JDBC是指 Java数据库的连接,是一种标准Java应用编程接口(JAVA API),用来连接Java编程语言和广泛的数据库
2.MySQL驱动包安装
2.1.mysql驱动包
JDBC编程需要用到mysql的驱动包(驱动包就是把mysql自身的api个转换成JDBC风格的)
2.2.驱动包及其下载
驱动包是由各个数据库官方提供的,本文用的的mysql驱动包
2.3安装具体流程
我们采取的是从maven中央仓库下载Maven Repository: Search/Browse/Explore (mvnrepository.com)
下载完之后就会有一个 jar 包 ,这个就和电脑上的zip压缩文件一样
到这一步mysql的驱动包就已经完成了!!!
3.引入依赖
3.1.创建项目
这个就不用我多说了吧,就是正常的project
3.2.导入依赖
1)随便创建一个目录test
2)复制刚刚的jar包
3)加入库add as library
4)完成!!!
4.JDBC代码的的基本流程(编写)
1)创建Datasource 对象,这个对象描述了数据库服务器在哪
DataSource dataSource = new MysqlDataSource();
2)详细描述数据库服务器在哪
//设置数据库所在的地址
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false"); // 向下转型
//设置登录数据库的用户名
((MysqlDataSource)dataSource).setUser("root");
//设置登录数据库的密码
((MysqlDataSource)dataSource).setPassword("wangbin");
详细了解MySQLURL!!!
MySQLURL:jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false1. jdbc:mysql:固定写法
2. 127.0.0.1:mysql服务器所在的主机ip,127.0.0.1是一个特殊的写法,表示 环回ip
3. 3306:端口号,表示mysql服务器的位置
4. characterEncoding=utf8:表示字符集为utf8形式
5.uesSSL = false表示不需要加密,true就表示加密,由于电脑并没有什么贵重的资源,也就不需要进行加密
3)数据库的连接
Connection connection = dataSource.getConnection();
//数据库的连接
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语句(此处演示delete)
String sql = "delete from student where id = ?";
6)讲过sql包装成一个语句对象
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);//替换上述索引
System.out.println(statement);//相当于一个日志
7)执行sql
int ret = statement.executeUpdate();
System.out.println(ret);
8)资源的释放
statement.close();
connection.close();
资源的释放类似于栈的原理,后使用的资源先释放,先使用的资源后释放
资源的释放可以将占用的资源释放回去,供其他人使用
以上八个操作基本 就已经写完一个基本的JDBC编程!!!下面让我来看完整的代码
5.JDBC的增删改
5.1insert 插入
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 {
//创建好数据源
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");
// 2.创建连接
Connection connection = dataSource.getConnection();
//用户的输入
Scanner scanner = new Scanner(System.in);
// 拼装sql语句
String sql = "insert into student values(?,?),(?,?),(?,?)";
//将SQL包装成一个语句对象
PreparedStatement statement = connection.prepareStatement(sql);
//进行替换操作
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); // 简单的日志
//执行sql
int ret = statement.executeUpdate();
System.out.println(ret);
//释放资源
statement.close();
connection.close();
}
}
5.2.delete删除
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的学生
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.update修改
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修改学生姓名
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("请输入学生id");
int id = scanner.nextInt();
System.out.println("请输入学生姓名");
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的select
select稍微和增删改有点不同
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 {
//创建数据源
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);
//执行SQL
ResultSet resultSet = statement.executeQuery();
//遍历结果结合,类似迭代器
//next 方法获取到一行记录,同时吧光标向后移动一行
while(resultSet.next()){
//针对当前这一行获取到每一列的信息
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println(id + name);
}
resultSet.close();
statement.close();
connection.close();
}
}
select操作需要用到resultSet.next()这个方法,类似于迭代器,获取到每一行的信息,资源的释放也是增加了一个
7.总结
对于初学者来说JDBC可能会比较的抽象,但是写了几遍后就会知道基本是一个模板,熟能生巧,加油