注意:最好用try...catch...finally代码块包裹
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://IP地址:端口号/数据库?useUnicode&characterEncoding=uft-8";
String user = "root";
String password = "root";
//创建连接对象
Connection connection = DriverManager.getConnection(url, user, password);
//创建执行对象
Statement statement = connection.createStatement();
//创建结果集对象,将结果存到结果集中
ResultSet resultSet = statement.executeQuery("select * from user");
//单行查询
while (resultSet.next()) {
System.out.println(resultSet.getInt(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3));
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public class Demo02 {
public static void main(String[] args) {
Demo02 demo02 = new Demo02();
//注意这里所传入的参数 如果是or '1' = '1' 条件则是正确的
System.out.println(demo02.verify("zxc", "1234 ' or '1' = '1"));
}
public boolean verify(String username_c, String password_c) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/web01?useUnicode&characterEncoding=utf-8";
String username = "root";
String password = "iloveyou";
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
String sql = "select * from user where username = '" + username_c + "' and password = '" + password_c + "';";
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
}
这段代码只要密码含有or ‘1‘ = ‘1‘,则会通过验证
为了解决sql注入问题,我们一般用PerparStatement作为执行对象
public boolean verify(String username_c, String password_c) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/web01?useUnicode&characterEncoding=utf-8";
String username = "root";
String password = "iloveyou";
connection = DriverManager.getConnection(url, username, password);
String sql = "select * from user where username = ? and password = ?";
preparedStatement = connection.prepareStatement(sql);
//设置?的参数
preparedStatement.setString(1, username_c);
preparedStatement.setString(2, password_c);
//上面已经执行过sql这里不用执行
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} 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();
}
}
}
return false;
}
}
用PreparedStatement就可以解决sql注入问题
select * from 表名 limit [位置偏移量], 行数
页是(查询页数-1)*行数, 行数
```javapublic class Demo03 {
public static void main(String[] args) {
Demo03 demo03 = new Demo03();
demo03.pageQuery(2, 4);
}
public void pageQuery(int pagenum, int linenum) {
pagenum = (pagenum - 1) * linenum;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/web01?useUnicode=true&CharacterEncoding=utf-8";
String username = "root";
String password = "iloveyou";
connection = DriverManager.getConnection(url, username, password);
String sql = "select * from user limit ?, ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, pagenum);
preparedStatement.setInt(2, linenum);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getInt(1) + " " + resultSet.getString(2) + " " + resultSet.getString(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} 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();
}
}
}
}
}
```
create read update delete
DML语言使用preparedStatement.executeUpdate();
preparedStatement.executeUpdate返回值 操作成功返回操作的行数,操作失败返回0
事物的开启:connection.setAutoCommit(false); //设置false之后,为手动提交
使徒的提交:connection.commit;
将所执行的sql代码放在其中
什么是连接池呢?
数据库的连接是非常耗费系统资源的,一个应用通常都是需要与数据库打交道,也就需要经常连接数据库,这样导致浪费大量系统资源;
连接池的原理就是:我事先创建好几个数据库连接放着,当我的系统需要操作数据库时就从连接池里直接拿连接,并将这个连接标记为 忙 ;用完后在放会池中,标记为 空闲;
当连接池里的连接都在被使用,如果此时还要连接,连接池就会在创建连接放到池里,这些连接的数量,都是在配置文件里由你控制的
public class Demo04 {
private static String url = "jdbc:mysql://localhost:3306/web01?useUnicode=true&characterEncoding=utf-8";
private static String username = "root";
private static String password = "iloveyou";
private static BasicDataSource basicDataSource;
static {
//获取数据源对象
basicDataSource = new BasicDataSource();
//加载驱动
basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
//设置连接
basicDataSource.setUrl(url);
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
//设置初始连接
basicDataSource.setInitialSize(5);
//设置最大连接 服务器承载的最大数
basicDataSource.setMaxTotal(20);
//设置空闲连接 防止有其他人进入时还得创建连接
basicDataSource.setMinIdle(3);
}
public static Connection getConnection() {
try {
return basicDataSource.getConnection(); //调用者可以直接close,该插件不会受影响(不会关闭)
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
原文:https://www.cnblogs.com/nightrainlemon/p/11650607.html