一、查询数据库的连接。
代码:
package com.xxx.jdbc;
import java.sql.*;
public class TestJdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="1127";
//1.加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库,代表数据库
Connection connection= DriverManager.getConnection(url,username,password);
//3.向数据库发送SQL的对象Statement:CRUD
Statement statement=connection.createStatement();
//4.编译SQL
String sql="select *from users";
String sql1="delete from users where id=4";
//受影响的行数,增删改都是用executeUpdate即可
// int i=statement.executeUpdate(sql1);
//5.执行查询sql,返回一个ResultSet:结果集
ResultSet resultSet= statement.executeQuery(sql);
while(resultSet.next()){
System.out.println("id+"+resultSet.getObject("id"));
System.out.println("name+"+resultSet.getObject("name"));
System.out.println("password+"+resultSet.getObject("password"));
System.out.println("email+"+resultSet.getObject("email"));
System.out.println("birthday+"+resultSet.getObject("birthday"));
}
//6.关闭连接,释放资源
resultSet.close();
statement.close();
connection.close();
}
}
二、增删改数据库
代码:
package com.xxx.jdbc;
import java.sql.*;
public class TestJDBC3 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="1127";
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库
Connection connection=DriverManager.getConnection(url,username,password);
//执行sql语句
String sql="insert into users(id,name,password,email,birthday) values(?,?,?,?,?)";
//预编译
PreparedStatement preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1,4);
preparedStatement.setString(2,"狂神焦");
preparedStatement.setString(3,"123456");
preparedStatement.setString(4,"2728443650@qq.com");
preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));
Statement statement=connection.createStatement();
//执行sql
int i=preparedStatement.executeUpdate();
if(i>0){
System.out.println("插入成功@");
}
//6.关闭连接,释放资源(一定要做)先开后关
preparedStatement.close();
connection.close();
}
}
三、事务数据库
代码
package com.xxx.jdbc;
import java.lang.reflect.Parameter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestJDBC2 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="1127";
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库
Connection connection= DriverManager.getConnection(url,username,password);
//通知数据库开启事务,false开启
connection.setAutoCommit(false);
String sql="update account set money=money-100 where name='A'";
PreparedStatement preparedStatement=connection.prepareStatement(sql);
preparedStatement.executeUpdate();
connection.commit();//以上两条sql都执行成功了,就提交事务
System.out.println("success");
connection.rollback();//如果出现异常,就通知数据库回滚事务
}
}