1
What problems can happen if I use Statement instead of Preparedstatement on the JDBC connection to the database
//Code using Preparedstatement, setting query values through methods
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM exemplo WHERE coluna1 = ? AND coluna2 = ?";
try {
con = DBConnection.getConnection();
ps = con.prepareStatement(query);
ps.setString(1, valorExemplo1);
ps.setString(2, valorExemplo2);
rs = ps.executeQuery();
//Code using Statement, concatenating the values of the variables to the query
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String query = "SELECT * FROM exemplo WHERE coluna1 = '"+valorExemplo1+"'
AND coluna2 = '"+valorExemplo2+"';
try{
con = DBConnection.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(query);
This serves to prevent attacks from SQL Injection <- this question is about C#, but the idea is the same
– hkotsubo