java操作数据库
? JDBC -> 持久层框架(myBatis/hibernate)
- 设计思想
- 操作(增删改/ 查询)
实现目标
-
java和数据库MySQL的连接(Connection) -
以参数方式实现DML操作 -
实现查询
将驱动文件引入到当前工程
- 将驱动文件文件拷贝到工程中
- 选择 Add as Library
- 在工程结构中查看添加情况:
连接示意图
创建连接(没有指定数据库)
public class ConnDemo {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306?useSSL=false&characterEncoding=UTF-8&serverTimezone=CST";
String username="root";
String password = "root";
connection = DriverManager.getConnection(url,username,password);
System.out.println("连接数据库成功");
} catch (ClassNotFoundException e) {
System.out.println("驱动程序不存在");
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
指定数据库,并完成添加操作
public class PreparedStatDemo {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/usermanager?useSSL=false&characterEncoding=UTF-8&serverTimezone=CST";
String username="root";
String password = "root";
connection = DriverManager.getConnection(url,username,password);
System.out.println("连接数据库成功");
String sql = "insert into manager values(NULL,'张三丰',2)";
preparedStatement = connection.prepareStatement(sql);
int i = preparedStatement.executeUpdate();
System.out.println("i:"+i);
} catch (ClassNotFoundException e) {
System.out.println("驱动程序不存在");
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
使用预编译方式实现添加
public class PreparedStatDemo2 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/usermanager?useSSL=false&characterEncoding=UTF-8&serverTimezone=CST";
String username="root";
String password = "root";
connection = DriverManager.getConnection(url,username,password);
System.out.println("连接数据库成功");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String name = scanner.nextLine();
System.out.println("请选择部门 1.开发一部 2.开发二部");
int depId = scanner.nextInt();
String sql = "insert into manager values(NULL,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setInt(2,depId);
int i = preparedStatement.executeUpdate();
System.out.println("i:"+i);
} catch (ClassNotFoundException e) {
System.out.println("驱动程序不存在");
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
查询场景分析:
-
带分页全查询 -
带条件的查询(登录) -
精确查询 -
模糊查询 ? select * from tableName where phone like ? ? prepStat.setObject(1,"%"+input+"%")
查询的基本操作
public class ResultSetDemo1 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/usermanager?useSSL=false&characterEncoding=UTF-8&serverTimezone=CST";
String username="root";
String password = "root";
connection = DriverManager.getConnection(url,username,password);
Scanner scanner = new Scanner(System.in);
System.out.println("请选择要查询部门 1.开发一部 2.开发二部");
int depId = scanner.nextInt();
String sql = "SELECT id,name 姓名,depid from manager where depid=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1,depId);
resultSet = preparedStatement.executeQuery();
boolean next = resultSet.next();
System.out.println("next:"+next);
if(next){
int id = resultSet.getInt("id");
System.out.println("id:"+id);
String name = resultSet.getString("姓名");
System.out.println("name:"+name);
}
} catch (ClassNotFoundException e) {
System.out.println("驱动程序不存在");
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
从结果集中获得特征
public class ResultSetDemo2 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
ResultSetMetaData metaData = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bj268?useSSL=false&characterEncoding=UTF-8&serverTimezone=CST";
String username="root";
String password = "root";
connection = DriverManager.getConnection(url,username,password);
String sql = "SELECT * from users";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
for (int i=1;i<=columnCount;i++){
System.out.print(metaData.getColumnLabel(i)+"\t");
}
System.out.println();
while(resultSet.next()){
for (int i=1;i<=columnCount;i++){
System.out.print(resultSet.getObject(i)+"\t");
}
System.out.println();
}
} catch (ClassNotFoundException e) {
System.out.println("驱动程序不存在");
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
把查询操作和集合结合
User.java:
public class User implements Serializable {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public User() {
}
}
public class ResultSetDemo3 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
ResultSetMetaData metaData = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bj268?useSSL=false&characterEncoding=UTF-8&serverTimezone=CST";
String username="root";
String password = "root";
connection = DriverManager.getConnection(url,username,password);
String sql = "SELECT * from users";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
List<User> users = new ArrayList<>();
while(resultSet.next()){
User user = new User();
user.setId(resultSet.getInt(1));
user.setName(resultSet.getString(2));
users.add(user);
}
System.out.println(users);
} catch (ClassNotFoundException e) {
System.out.println("驱动程序不存在");
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
注意:回收时要与创建相颠倒
|