zoukankan      html  css  js  c++  java
  • java JDBC (二) 防止注入/参数化

    package cn.sasa.demo2;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Scanner;
    
    public class JDBCDemo2 {
    
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            /**
             * 防止注入攻击 /参数化查询
             * Statement 接口 有个子接口 PreparedStatement --sql预编译
             * 
             */
            String name = "";
            String pwd = "";
            Scanner sc = new Scanner(System.in);
            System.out.println("用户名");
            name = sc.nextLine();
            System.out.println("密码");
            pwd = sc.nextLine();
            
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.0.207:3306/mydb", "root", "XXXXXXXX1");
            
            //String sql = " SELECT * FROM `user` WHERE name='"+ name +"' and pwd='"+ pwd +"' ";
            //Statement state = conn.createStatement();
            //ResultSet rsSet = state.executeQuery(sql);
            
            String sql = " SELECT * FROM `user` WHERE name=? and pwd=? ";
            PreparedStatement pstate = conn.prepareStatement(sql);
            pstate.setObject(1, name);
            pstate.setObject(2, pwd);
            ResultSet rsSet = pstate.executeQuery();
            
            while(rsSet.next()) {
                System.out.println(rsSet.getString("name"));
            }
            
            rsSet.close();
            //state.close();
            pstate.cancel();
            conn.close();
            
        }
    
    }
  • 相关阅读:
    (SenchaTouch+PhoneGap)开发笔记(2)开发环境搭建二
    Sql语句复习
    冒泡排序
    微信开发订阅号(ASP.NET MVC4+jquery mobile+AppHarbor发布)
    Ext4 ComboBox组件使用
    ExtJs 进度条(轮询)
    如何替换掉.net toolStrip控件溢出按钮背景图
    easyui-menu 宽度自适应
    Python之入门学习
    servlet和filter的区别
  • 原文地址:https://www.cnblogs.com/SasaL/p/10233412.html
Copyright © 2011-2022 走看看