一、JDBC简介
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
public static void main(String[] args) throws Exception {
//1 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2 获取连接
//如果连接的是本机mysql并且端口是默认的3306,可以简化书写:
//String url="jdbc:mysql://127.0.0.1:3306/luhao";
String url="jdbc:mysql:///luhao";
String username="root";
String password="121262";
Connection conn = DriverManager.getConnection(url, username, password);
//定义sql
String sql="update emp set workname = \"无脑工作\" where number=1001";
//获取执行sql的对象 statement
Statement stmt=conn.createStatement();
//执行sql
int count=stmt.executeUpdate(sql);
System.out.println(count);
//释放资源
stmt.close();
conn.close();
}
}
二、API详解--DriverManager
(一)注册驱动
随着静态类的加载而直接完成注册驱动
(二)获取数据库连接
三、API详解--Connection
(一)获取执行SQL的对象
①createStatement()
②prepareStatement(sql)
预编译
SQL注入+PrepareStatement演示:
import java.sql.*;
public class Text_prepareStatement {
public static void main(String[] args) throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/luhao?useServerPrepStmts=true"; //jdbc:mysql+ ip 地址 + 端口号 +数据库名称
// useServerPrepStmts=true 开启预编译
String useraccount="root";
String userpassword="121262";
//2.获取连接
Connection conn = DriverManager.getConnection(url, useraccount, userpassword);
/* String account="sasdasd";
String password="1245";*/
//演示sql注入
String account="asdasdsdF";
String password="' or '1' ='1";
String account1="ASDSAD";
String password1="' or '1' ='1";
//定义sql命令(statement版本)
/*String sql="Select * from login where account='"+account+"' and password='"+password+"'";*/
//打印sql语句 查看sql注入的影响
/*System.out.println(sql);*/
//Select * from login where account='asdasdsdF' and password='' or '1' ='1'
//获取Statement对象
/*Statement stmt = conn.createStatement();*/
//定义sql命令(preparestatement版本)
String sql="Select * from login where account= ? and password= ?";
//获取prepareStatement
PreparedStatement pstmt = conn.prepareStatement(sql);
//设置 ? 的值
//第一个和第二个?对应的值都是String
//采用setString方法
pstmt.setString(1,account);
pstmt.setString(2,password);
ResultSet resultSet = pstmt.executeQuery();
//执行sql语句
//返回值:受sql语句影响的数据数量
/*ResultSet resultSet = stmt.executeQuery(sql);*/
//检测是否需要再次编译
pstmt.setString(1,account1);
pstmt.setString(2,password1);
//执行sql语句
resultSet = pstmt.executeQuery();
if(resultSet.next()){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
//登录成功(statement版本)
//登录失败(preparestatement版本)
//释放资源
//stmt.close();
pstmt.close();
conn.close();
}
}
(二)管理事务
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Text_connection {
public static void main(String[] args) throws Exception {
//1 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2 获取连接
//如果连接的是本机mysql并且端口是默认的3306,可以简化书写:
//String url="jdbc:mysql://127.0.0.1:3306/luhao";
String url="jdbc:mysql:///luhao";
String username="root";
String password="121262";
Connection conn = DriverManager.getConnection(url, username, password);
//定义sql
String sql="update emp set workname = \'无工作\' where number=1001";
String sql2="update emp set workname = \'带脑工作\' where number=1002";
//获取执行sql的对象 statement
Statement stmt=conn.createStatement();
//开启事物
try {
//开启事物
conn.setAutoCommit(false);
//执行sql
int count=stmt.executeUpdate(sql);
System.out.println(count);
int i=3/0;
int count2=stmt.executeUpdate(sql2);
System.out.println(count2);
提交事物
conn.commit();
} catch (Exception e) {
//回滚事物
conn.rollback();
e.printStackTrace();
}
//释放资源
stmt.close();
conn.close();
}
}
四、API详解--ResultSet
import java.sql.*;
public class Text_resultset {
public static void main(String[] args) throws Exception {
//1 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2 获取连接
//如果连接的是本机mysql并且端口是默认的3306,可以简化书写:
//String url="jdbc:mysql://127.0.0.1:3306/luhao";
String url="jdbc:mysql:///luhao";
String username="root";
String password="121262";
Connection conn = DriverManager.getConnection(url, username, password);
//定义sql
String sql3="select * from emp";
//获取执行sql的对象 statement
Statement stmt=conn.createStatement();
//执行结果 获取表中的所有数据
ResultSet resultSet = stmt.executeQuery(sql3);
while(resultSet.next()){
//根据字段类型和顺序来记录数据
int anInt = resultSet.getInt(1);
String string = resultSet.getString(2);
String string1 = resultSet.getString(3);
String string2 = resultSet.getString(4);
String string3 = resultSet.getString(5);
Date date = resultSet.getDate(6);
int anInt1 = resultSet.getInt(7);
System.out.println(anInt);
System.out.println(string);
System.out.println(string1);
System.out.println(string2);
System.out.println(string3);
System.out.println(date);
System.out.println(anInt1);
}
System.out.println(resultSet.toString());
//释放资源
stmt.close();
conn.close();
}
}