jdbc操作mysql数据库(防止注入攻击版本)
2017-04-10 18:07
411 查看
package TestJDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class SQLDemo { public static void main(String[] args) { SQLDemo demo = new SQLDemo(); //demo.login("a' or 'a'='a", "a' or 'a'='a"); demo.login("zs", "zs"); } public static Connection getConnection() throws Exception{ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/web08"; return DriverManager.getConnection(url, "root", "root"); } public void login(String username,String password){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = getConnection(); String sql = "select * from user where username=? and password=?"; ps = conn.prepareStatement(sql); ps.setString(1, username); ps.setString(2, password); rs = ps.executeQuery(); if(rs.next()){ System.out.println("欢迎:"+rs.getString("username")); }else { System.out.println("用户名或密码错误"); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(conn!=null) conn.close(); }catch(Exception e){ e.printStackTrace(); } } } }
相关文章推荐
- {php 编程下有效防止MySQL数据库注入攻击}
- php 编程下有效防止MySQL数据库注入攻击
- 在ASP.NET中防止注入攻击[翻译]
- 在ASP.NET中防止注入攻击
- 在ASP.NET中防止注入攻击[翻译]
- asp中防止脚本注入攻击
- 防止GET,Post注入和防止服务器攻击,并记录IP
- 防止"SQL的注入攻击"
- ASP.NET中防止注入攻击
- 在ASP.NET中防止注入攻击[翻译] .doc
- 在ASP.NET中防止注入攻击[翻译]
- ASP.NET中防止注入攻击
- 在ASP.NET中防止注入攻击
- asp中防止脚本注入攻击
- JSP数据库操作例程 - 存储过程 - JDBC-ODBC - SQL Server - 1.1版本
- 在asp.net中防止注入攻击
- 在ASP.NET中防止注入攻击[翻译]
- 在ASP.NET中防止注入攻击
- 在ASP.NET中防止注入攻击[翻译]
- 在ASP.NET中防止注入攻击