4.11 Statement, PreparedStatement and CallableStatement
Statement
PreparedStatement
public class PreparedStatementDemo { public static void main(String args[]) throws SQLException { String queryBillingType = "select distinct billing_type from billing where bank = ?"; Connection conn = DriverManager.getConnection("mysql:\\localhost:3303", "admin", "admin"); PreparedStatement preparedStatement = conn.prepareStatement(queryBillingType); preparedStatement.setString(1, "Citibank"); ResultSet result = preparedStatement.executeQuery(); while(result.next()){ System.out.println("Billing Type: " + result.getString("billing_type")); } } }
String loanType = getLoanType(); PreparedStatement prestmt = conn.prepareStatement("select banks from billing where billing_type = " + billingType);PreparedStatement prestmt = conn.prepareStatement("select banks from billing where billing_type = ?"); prestmt.setString(1, billingType);
sql = "SELECT * FROM users WHERE name = '" + userName + "' and password = '"+ passWord +"';";userName = "1' OR '1' = '1"; passWord = "1' OR '1' = '1";sql = "SELECT * FROM users WHERE name = '1' OR '1'='1' and password = '1' OR '1'='1';";sql = "SELECT * FROM users";sql = "SELECT * FROM users WHERE name = '" + userName + "';";userName = " 1' OR 1=1";userName = " 1'' OR 1=1";sql = "SELECT * FROM users WHERE name = '1'' OR 1=1';";
sql = "SELECT * FROM billing WHERE billing_type IN (?)"; preparedSatement.setString(1, "'credit card', 'cash', 'apple pay'");
CallableStatement
Last updated