时间:2021-07-01 10:21:17 帮助过:5人阅读
2 JDBC的工具类
package day_19; import java.io.InputStream; import java.sql.*; import java.util.Properties; /**操纵JDBC的工具类,其中封装了一些工具方法 * Version 1 : getConnection() : 通过读取配置文件从数据库服务器获取一个连接; * Version 2 : release() : 关闭数据库资源的ResultSet/Statement/Statement * Version 3 : update_sql() : 添加preparedStatement的select的sql的方法 * Version 4: query_sql() : 执行preparedStatement的的查询操作! */ public class JDBCTools { /*** 执行sql 语句,使用Preparedstatement * @param sql * @param args */ public static void update_sql(String sql,Object...args){//添加preparedStatement的update更新sql的方法,同理可以实现 : 增删改 Connection connection=null; PreparedStatement preparedStatement=null; try { connection = JDBCTools.getConnection(); preparedStatement=connection.prepareStatement(sql); for(int i=0;i<args.length;i++){ preparedStatement.setObject(i+1, args[i]); } preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { release(null,preparedStatement ,connection); } } //添加preparedStatement的select的sql的方法 public static void query_sql(String sql,Object...args){//执行preparedStatement的的查询操作! Connection connection=null; PreparedStatement preparedStatement=null; try { connection = JDBCTools.getConnection(); preparedStatement=connection.prepareStatement(sql); for(int i=0;i<args.length;i++){ preparedStatement.setObject(i+1, args[i]); } ResultSet resultSet=null; resultSet=preparedStatement.executeQuery();//执行查询操作! if(resultSet.next()) System.out.println("RsultSet查询已经就绪!"); else System.out.println("数据表为空或者404!"); } catch (Exception e) { e.printStackTrace(); } finally { release(null,preparedStatement ,connection); } } public static void release(ResultSet rs,Statement statement, Connection conn){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e2) { e2.printStackTrace(); } } } public static Connection getConnection() throws Exception{ //1.准备数据库的连接的四个字符串 String driverClass=null,jdbcUrl=null,user=null,password=null; //jdbc:mysql:///books ;也可以将localhost省略掉! //2.读取类路径下的jdbc.properties 文件 InputStream in= JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties =new Properties(); properties.load(in); driverClass =properties.getProperty("driver"); jdbcUrl=properties.getProperty("jdbcUrl"); user = properties.getProperty("user"); password = properties.getProperty("password"); //3.加载数据库驱动程序(注册驱动),driver对应的实现类中有注册驱动的静态代码块 // Class.forName(driverClass); // //或这么手动加载,也可以注册多个数据库连接的代码块 //DriverManager.registerDriver( Class.forName(driverClass).newInstance()); //4.通过DriverManager 的getConnection()方法获取数据库连接。 Connection connection=DriverManager.getConnection(jdbcUrl,user,password); System.out.print(connection); //com.mysql.jdbc.JDBC4Connection@19e1023e return connection; } }
3 testSQLInjection
package day_19; import org.junit.Test; import javax.swing.text.DocumentFilter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class testSQLInjection { /**使用 * */ @Test //因为匹配结果恒真,就可以进行SQL注入!使用PreparedStatement可以避免这个问题! public void testSQLInjection(){ String username = "a‘ OR PASSWORD = "; String password = " OR ‘1‘=‘1"; String sql = "SELECT * FROM users WHERE username = ‘" + username + "‘ AND " + "password = ‘" + password + "‘"; System.out.println(sql); Connection connection=null; Statement statement=null; ResultSet resultSet=null; try { connection=JDBCTools.getConnection(); statement=connection.createStatement(); resultSet=statement.executeQuery(sql); if(resultSet.next()) System.out.println("登陆成功!"); else System.out.println("404 ! "); /*******/ System.out.println("PreparedStatement测试结果为:"); String sql2 = "SELECT * FROM users WHERE username = ? AND password = ?"; JDBCTools.query_sql(sql2,username,password); } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.release(resultSet, statement,connection ); } } }
JDBC课程4--使用PreparedStatement进行增删查改, JDBCTools新增对应的的功能,模拟SQL注入
标签:河北 loader for 资源 public result turn strong ace