<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
dependencies>
https://search.maven.org/
可供大家查找想要的依赖jar 包的属性。
//1.加载数据库驱动(MySQL)备注:5.1之后可以不明确加载驱动
Class.forName( "com.mysql.jdbc.Driver" );
//2.连接数据库
//jdbc:database://host:port/databaseName?p1=v1&p2=v2
//jdbc:mysql://localhost:3306/student_management
Connection connection =
DriverManager.getConnection( "jdbc:mysql://localhost:3306/studentdatabase?user=root&password=123456&useSSL=false" );
//3.创建 Statement 命令
Statement statement = connection.createStatement();
这种情况下创建的操作命令执行带参数的SQL语句会发生SQL注入。
//通过调用方法传入参数执行SQL语句
public static void queryMemoGroupByName(String groupName) {
String sql = "select * from 表名 where 字段 in (?)";
PreparedStatement preparedStatement = connection.prepareStatement( sql );
preparedStatement.setString( 1, groupName );
}
//4.准备SQl语句执行SQL语句
//R 查询
ResultSet resultSet = statement.executeQuery( "select * from 表名" );
ResultSet resultSet = preparedStatement.executeQuery();
//CUD 更新 删除 增加操作
int insertValue =statement.executeUpdate("insert into 表名 values ( )" );
int insertValue =preparedStatement.executeUpdate("insert into 表名 values ( )" );
while (resultSet.next()) {//如果返回true表示有下一行记录,则否无记录
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
LocalDateTime createdTime = resultSet.getTimestamp("created_time").toLocalDateTime();
LocalDateTime modifyTime = resultSet.getTimestamp("modify_time").toLocalDateTime();
System.out.println(String.format("编号:%d, 名称:%s, 创建时间:%s, 修改时间:%s",
id, name,createdTime.toString(), modifyTime.toString()
));
}
//关闭结果集 关闭命令 关闭连接
resultSet.close();
statement.close();
connection.close();
//AutoCloseable接口 自动关闭
try(
Connection connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/databaseName?user=root&password=123456&useSSL=false" );
//3.创建命令
Statement statement = connection.createStatement();
//4.准备SQl语句
ResultSet resultSet = statement.executeQuery( "select * from 表名" )
)
import java.sql.*;
/**
* @Auther: SolarL
* @Date: 2018/11/24
* @Description: com.sunlong.jdbc
* @version: 1.0
*/
public class TestJDBC3 {
public static void queryMemoGroupByName(String groupName) {
try {
Class.forName( "com.mysql.jdbc.Driver" );
Connection connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/studentdatabase?user=root&password=123456" );
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery( "select * from student_management where 学号 = '" + groupName + "'" );
while (resultSet.next()) {
String num = resultSet.getString( "学号" );
String name = resultSet.getString( "姓名" );
String sex = resultSet.getString( "性别" );
String school = resultSet.getString( "学院" );
String class1 = resultSet.getString( "班级" );
String class2 = resultSet.getString( "籍贯" );
System.out.println( String.format(
"学号:%s,姓名:%s,性别:%s,学院:%s,班级:%s,籍贯:%s",
num, name, sex, school, class1, class2
) );
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// queryMemoGroupByName( "201612345678" );
//SQL注入
//select * from student_management where 学号 = '+ groupName+';
//select * from student_management where 学号= 'PHP组' or 1=1 or 1='';
queryMemoGroupByName( "学生组' or 1=1 or 1='" );
}
}
对于以上SQL注入问题的解决办法,只需要将创建操作命令的时候使用 PreparedStatement 创建即可。
import java.sql.*;
/**
* @Auther: SolarL
* @Date: 2018/11/25
* @Description: com.sunlong.jdbc
* @version: 1.0
*/
public abstract class JdbcTemplate {
private String url;
private Connection connection;
private Statement statement;
private ResultSet resultSet;
Integer effect = -1;
//jdbc:mysql://localhost:3306/database?user=root&password=018162
public JdbcTemplate(String host, Integer port, String databaseName, String user, String password) {
this.url = String.format( "jdbc:mysql://%s:%d/%s?user=%s&password=%s", host, port, databaseName, user, password );
}
public final void call() {
//1.加载驱动
loadDriver();
//2.创建连接
createConnect();
//3.创建命令
createStatement();
//4.准备SQL
createSql();
//5.执行SQL
execute();
//6.处理结果
//第一类:CUD int 第二: R result
handlerResult();
//7.关闭结果集 关闭命令 关闭连接
closeAll();
}
private void closeAll() {
if (this.resultSet != null) {
try {
this.resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (this.statement != null) {
try {
this.statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (this.connection != null) {
try {
this.connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
this.effect = -1;
}
private void handlerResult() {
if (this.executeType()) {
try {
this.handlerR( resultSet );
} catch (SQLException e) {
e.printStackTrace();
}
} else {
this.handleCUD( effect );
}
}
protected abstract void handleCUD(Integer effect);
protected abstract void handlerR(ResultSet resultSet) throws SQLException;
private void execute() {
String sql = this.createSql();
if (sql != null) {
if (this.executeType()) {
try {
resultSet = statement.executeQuery( sql );
} catch (SQLException e) {
e.printStackTrace();
}
} else {
try {
effect = statement.executeUpdate( sql );
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//如果返回true 查询, 如果返回false 插入,删除,更新
public abstract boolean executeType();
//用户来覆写
public abstract String createSql();
private void createStatement() {
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void createConnect() {
try {
connection = DriverManager.getConnection( this.url );
} catch (SQLException e) {
e.printStackTrace();
}
}
private void loadDriver() {
try {
Class.forName( "com.mysql.jdbc.Driver" );
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
/**
* @Auther: SolarL
* @Date: 2018/11/26
* @Description: com.sunlong.jdbc
* @version: 1.0
*/
public class TestJdbcTemplate {
public static void main(String[] args) {
JdbcTemplate jdbcTemplate = new JdbcTemplate( "localhost", 3306, "memo", "root", "123456" ) {
@Override
protected void handleCUD(Integer effect) {
//do something
}
@Override
protected void handlerR(ResultSet resultSet) throws SQLException {
if (resultSet != null) {
while (resultSet.next()) {
int id = resultSet.getInt( "id" );
String name = resultSet.getString( "name" );
LocalDateTime createdTime = resultSet.getTimestamp( "created_time" ).toLocalDateTime();
LocalDateTime modifyTime = resultSet.getTimestamp( "modify_time" ).toLocalDateTime();
System.out.println(
String.format(
"编号:%d, 名称:%s, 创建时间:%s, 修改时间:%s",
id, name,
createdTime.toString(),
modifyTime.toString()
) );
}
}
}
@Override
public boolean executeType() {
return true;
}
@Override
public String createSql() {
return "select id,name,created_time,modify_time from memo_group ";
}
};
jdbcTemplate.call();
}
}
import java.sql.*;
/**
* @Auther: SolarL
* @Date: 2018/11/25
* @Description: com.sunlong.jdbc
* @version: 1.0
*/
public class TransactionJdbc {
public static void main(String[] args) {
//演示事务
//1.更新操作 - 正常
//2.插入操作 - 错误
transaction();
}
public static void transaction() {
Connection connection = null;
try {
//1、加载数据库驱动
Class.forName( "com.mysql.jdbc.Driver" );
//2.建立连接,连接数据库
//关于数据库连接的URL格式JDBC规范里面也有定义
// jdbc:database://host:port/databaseName?p1=v1&p2=v2
//jdbc:mysql://localhost:3306/memo?user=root&password=123456
connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/studentdatabase?user=root&password=018162" );
connection.setAutoCommit( false );
//3.创建命令
String updateSQL = " update student_management set 班级 = ? where 学号 = ?";
PreparedStatement preparedStatement = connection.prepareStatement( updateSQL );
preparedStatement.setString( 1, "信息163" );
preparedStatement.setString( 2, "201611010321" );
//4.准备SQL并执行
int effectUpdate = preparedStatement.executeUpdate();
System.out.println( "更新操作" + (effectUpdate == 1) );
String insertSQL = "insert into student_management(id,name) values (?,?)";
preparedStatement = connection.prepareStatement( insertSQL );
int effectInsert = preparedStatement.executeUpdate();
System.out.println( "插入操作:" + (effectInsert == 1) );
if (effectUpdate == 1 && effectInsert == 1) {
connection.commit();
} else {
connection.rollback();
}
//7.关闭命令
preparedStatement.close();
//8.关闭连接
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
if (connection != null) {
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
}