MySQL_(Java)使用JDBC向数据库发起查询请求  传送门

  MySQL_(Java)使用JDBC创建用户名和密码校验查询方法  传送门

  MySQL数据库中的数据,数据库名garysql,表名garytb,数据库中存在的用户表

  

  存在SQL注入问题

  使用preparestatement做查询语句时可解决SQL注入的问题

  pstmt.setString(1, username)将username作为一个结果传入到"where username = ?"的问号中

String sql = "select * from garytb where username = ? and password = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
//添加参数
pstmt.setString(1, username);
pstmt.setString(2, password);
//进行查询
rs = pstmt.executeQuery(); if(rs.next()) {
return true;
}else {
return false;
}

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class JDBC01 { public static void main(String[] args) throws SQLException {
//selectAll();
//存在sql注入
System.out.println(selectByUernamePassword("Garyyyyar","nihao' or '1'='1"));
//使用preparestatement解决SQL注入的问题
System.out.println(selectByUP2("Garyyyyar","nihao' or '1'='1"));
} public static void selectAll() throws SQLException {
//注册驱动 使用驱动连接数据库
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver"); //String url ="jdbc:mysql://localhost:3306/garysql";
//指定编码查询数据库
String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
String user = "root";
String password = "123456";
//建立和数据库的连接
con = DriverManager.getConnection(url,user,password); //数据库的增删改查
stmt = con.createStatement();
//返回一个结果集
rs =stmt.executeQuery("select * from garytb"); while(rs.next()) {
//System.out.println(rs.getString(1)+","+rs.getString(2)+","+rs.getString(3));
System.out.println(rs.getString("id")+","+rs.getString("username")+","+rs.getString("password"));
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
} public static boolean selectByUernamePassword(String username,String password) throws SQLException {
Connection con=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver"); String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
con = DriverManager.getConnection(url,"root","123456");
stmt =con.createStatement();
String sql = "select * from garytb where username = '"+username+"' and password = '"+password+"'";
//System.out.println(sql);
rs = stmt.executeQuery(sql); if(rs.next()) {
return true;
}else {
return false;
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
} return false;
} public static boolean selectByUP2(String username,String password) throws SQLException{
Connection con=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver"); String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
con = DriverManager.getConnection(url,"root","123456"); String sql = "select * from garytb where username = ? and password = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
//添加参数
pstmt.setString(1, username);
pstmt.setString(2, password);
//进行查询
rs = pstmt.executeQuery(); if(rs.next()) {
return true;
}else {
return false;
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
} return false;
}
}

JDBC01.java

public static boolean selectByUP2(String username,String password) throws SQLException{
Connection con=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver"); String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
con = DriverManager.getConnection(url,"root","123456"); String sql = "select * from garytb where username = ? and password = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
//添加参数
pstmt.setString(1, username);
pstmt.setString(2, password);
//进行查询
rs = pstmt.executeQuery(); if(rs.next()) {
return true;
}else {
return false;
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
} return false;
}

MySQL_(Java)使用preparestatement解决SQL注入的问题的更多相关文章

  1. MyBatis是如何解决Sql注入的

    转:[转]mybatis如何防止sql注入 java中预处理PrepareStatement为什么能起到防止SQL注入的作用??!! 一.SQL注入 sql注入大家都不陌生,是一种常见的攻击方式,攻击 ...

  2. JDBC_08_解决SQL注入问题 (登录和注册)

    解决SQL注入问题 只要用户提供的信息不参与sql语句的编译过程,那么尽管用户输入的信息中含有sql关键字那么也不会起作用了 要想使用户提供信息不参与sql语句的编译过程,那么必须使用 java.sq ...

  3. jdbc 07: 解决sql注入

    jdbc连接mysql,解决sql注入问题 package com.examples.jdbc.o7_解决sql注入; import java.sql.*; import java.util.Hash ...

  4. PreparedStatement解决sql注入问题

    总结 PreparedStatement解决sql注入问题 :sql中使用?做占位符 2.得到PreparedStatement对象 PreparedStatement pst=conn.prepar ...

  5. 使用过滤器解决SQL注入和跨站点脚本编制

    1 SQL注入.盲注 1.1 SQL注入.盲注概述 Web 应用程序通常在后端使用数据库,以与企业数据仓库交互.查询数据库事实上的标准语言是 SQL(各大数据库供应商都有自己的不同版本).Web 应用 ...

  6. IBatis.Net使用总结(一)-- IBatis解决SQL注入(#与$的区别)

    IBatis解决SQL注入(#与$的区别) 在IBatis中,我们使用SqlMap进行Sql查询时,需要引用参数,在参数引用中可以使用两种占位符#和$.这两种占位符有什么区别呢? (1):#***#, ...

  7. 解决 SQL 注入的另类方法

    本文是翻译,版权归原作者所有 原文地址(original source):https://bitcoinrevolt.wordpress.com/2016/03/08/solving-the-prob ...

  8. Java学习之路- SQL注入

    用户名: __________ 密码:——————— 假如没有使用预处理的Statement 对象 拼接字符串查数据库的话,易收到sql注入攻击: 例如说 : mysql 中   #代表的是单行注释 ...

  9. [转]开发中如何解决SQL注入的问题

    Java防止SQL注入 SQL 注入简介:        SQL注入是最常见的攻击方式之一,它不是利用操作系统或其它系统的漏洞来实现攻击的,而是程序员因为没有做好判断,被不法用户钻了SQL的空子,下面 ...

随机推荐

  1. 用eclipse启动tomcat时报Could not publish server configuration for Tomcat v8.0 Server at localhost..错误

    网上的解决方法是: 1.如果是使用的eclipse tomcat 插件,需要在你的工作空间 找到如下文件:.metadata.plugins\org.eclipse.wst.server.cor\e\ ...

  2. Aveva Marine 新建项目001

    1# 项目代号定义,三个字符,例如Abc 2# 新建文件夹,命名为“Abc” 3# 新建文件名为evars.bat文件,放到项目文件夹的根目录 内容为: SET Abc000=项目文件夹路径\Abc0 ...

  3. JS基础_for循环练习3

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 【ExtJs】在Ext.grid.Panel中,两列的值相乘作为第三列的值的实现

    如: 商品总价=商品单价*商品数量 方法: 商品总价列,使用其renderer属性,为期定义一个方法,该方法将当前record中的另外两列中2个数据相乘后渲染到该商品总价列.

  5. 小程序API接口调用

    1.在config.js中写入api接口及appkey   2.在HTTP.js中引入config.js,然后新建HTTP.js,在里进行wx.request的封装. 定义一个HTTP的类,来类里定义 ...

  6. Base64加密后有换行回车的解决办法

    据RFC 822规定,每76个字符,还需要加上一个回车换行 有时就因为这些换行弄得出了问题,解决办法如下,替换所有换行和回车 String bTemp = Base64.encodeBase64Str ...

  7. 如何将spring源码导入到eclipse中

    如何将spring源码导入到eclipse中 1. 下载spring源码  可以在github官网中找到spring源码来下载,或者直接通过git下载,是一样的,这里演示 直接在github网站下载, ...

  8. 保证在浏览器上word/图片/Excel的下载的表现形式一样

    function downloadImage(src) { console.log(src); //src="http://192.168.12.50:8181/file/common/pn ...

  9. C++typedef struct和struct的区别

    #include "pch.h" #include struct struct1 { int a; char b; char* c; }test1;//定义结构体变量 typede ...

  10. 快速导入Excel数据到mysql

    首先利用mysql文件,导出csv文件, 然后,直接修改csv文件,然后导入csv文件