模板语句
DriverManager
加载驱动默认方法语句 通常不这么写,知道就可以
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
加载驱动固定写法
Class.forName("com.mysql.jdbc.Driver");
获取数据库驱动对象
Connection connection = DriverManager.getConnection(url,username,password);
connection代表数据库 设置数据库自动提交 事物提交 事物回滚
URL
jdbc:mysql://主机IP:端口/数据库名?
String url = "jdbc:mysql://82.157.178.132:3306/mydb?userUnicode=true&characterEncoding=utf-8&useSSL=true";
Statement
执行类:执行SQL的对象
statement.executeUpdate();
statement.executeQuery();
statement.executeBatch();
statement.execute();
ResultSet
查询结果集,封装了所有的查询结果
resultSet.getObject();
resultSet.getInt();
resultSet.getFloat();
resultSet.getString();
resultSet.getDate();
resultSet.beforeFirst();
resultSet.afterLast();
resultSet.next();
resultSet.previous();
resultSet.absolute();
开始编
配置文件db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://82.157.178.132:3306/mydb?userUnicode=true&characterEncoding=utf-8&useSSL=true
username=weijun901
password=123456
工具类JdbcUtils.java
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
static {
try {
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("./MySQL/db.properties");
Properties properties = new Properties();
properties.load(in);
properties.getProperty("driver");
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
public static void release(Connection conn, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
不安全的Statement (MySQL注入)
增Insert.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Insert {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "insert into user(id,`username`,`password`)\n" + "values (7,'weijun901','123456')";
int i = st.executeUpdate(sql);
if (i > 0) {
System.out.println("插入成功");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, null);
}
}
}
删Delete.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Delete {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "delete from user where id = 7";
int i = st.executeUpdate(sql);
if (i > 0) {
System.out.println("删除成功");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, null);
}
}
}
改Update.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Update {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "update user set `userName` = 'weijun',`password`='654321' where id=7";
int i = st.executeUpdate(sql);
if (i > 0) {
System.out.println("修改成功");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, null);
}
}
}
查Query.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Query {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "select * from user where id=7";
rs = st.executeQuery(sql);
if (rs.next()) {
System.out.println(rs.getString("id"));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, rs);
}
}
}
登录业务Login.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Login {
public static void main(String[] args) {
login("'or '1=1", "'or '1=1");
}
public static void login(String username, String password) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
st = conn.createStatement();
String sql = "select * from user where `username`='" + username + "' AND `password` = '" + password + "'";
rs = st.executeQuery(sql);
if (rs.next()) {
System.out.println(rs.getString("id"));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, rs);
}
}
}
安全的PreparedStatement
数据库增加了date字段
增InsertMySQL.java
import java.util.Date;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class InsertMySQL {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "insert into user(`id`,`username`,`password`,`date`) values(?,?,?,?)";
st = conn.prepareStatement(sql);
st.setInt(1, 8);
st.setString(2, "jinting1027");
st.setString(3, "654321");
st.setDate(4, new java.sql.Date(new Date().getTime()));
int i = st.executeUpdate();
if (i > 0) {
System.out.println("插入成功");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, null);
}
}
}
删DeleteMySQL.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DeleteMySQL {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "delete from user where id=?";
st = conn.prepareStatement(sql);
st.setInt(1, 8);
int i = st.executeUpdate();
if (i > 0) {
System.out.println("删除成功");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, null);
}
}
}
改UpdateMySQL.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UpdateMySQL {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "update user set `username`=? where id=?;";
st = conn.prepareStatement(sql);
st.setString(1, "jinting1027");
st.setInt(2, 7);
int i = st.executeUpdate();
if (i > 0) {
System.out.println("更新成功");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, null);
}
}
}
查QueryMySQL.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class QueryMySQL {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "select * from user where id = ?";
st = conn.prepareStatement(sql);
st.setInt(1, 7);
rs = st.executeQuery();
if (rs.next()) {
System.out.println(rs.getString("id"));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
System.out.println(rs.getString("date"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, rs);
}
}
}
登录业务LoginMySQL.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginMySQL {
public static void main(String[] args) {
login("weijun","654321");
}
public static void login(String username, String password) {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
String sql = "select * from user where `username`=? and `password`=?";
st = conn.prepareStatement(sql);
st.setString(1,username);
st.setString(2,password);
rs = st.executeQuery();
if (rs.next()) {
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
System.out.println(rs.getDate("date"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.release(conn, st, rs);
}
}
}
|