What's new

Closed Tip: always use (or practice using) parameters

Status
Not open for further replies.

sum1

Addict
Joined
Jan 24, 2017
Posts
170
Reaction
24
Points
95
Bakit?
Dahil mas maayos basahin ito:

(C#) String a = String.format("Hi! Mr.{0}, the item {1} you ordered will be delivered within {2} day(s).", _last_name,
_item,
_days_left);
//ang index ng _last_name ay 0
//ang index ng _item ay 1
//ang index ng _days_left ay 2

(Java)String a = String.format("Hi! Mr.%1$s, the item %2$s you ordered will be delivered within %3$d day(s).", _last_name, _item, _days_left);
//ang index ng _last_name ay 1
//ang index ng _item ay 2
//ang index ng _days_left ay 3
//ang minimum index sa java ay 1 hindi 0
//ang s ay string
//ang d ay integer o decimal


kaysa dito:
String a = "Hi! Mr. "+ _last_name + ", the item " +_item + " you ordered will be delivered within "+ _days_left + " day(s).";

Bukod pa dyan maganda din yan mas lalo na sa mga database queries para makaiwas sa SQL Injections
ex:

(C#)MySqlCommand command0 = new MySqlCommand("SELECT * FROM tbl_person WHERE person_id = @id", con);
command0.Parameters.Add("@id", MySqlDbType.int32).Value = _id;

(Java)PreparedStatement command0 = con.prepareStatement("SELECT * FROM tbl_person WHERE person_id = ?");
command0.setInt(1, _id);


traditional:

(C#)MySqlCommand command0 = new MySqlCommand("SELECT * FROM tbl_person WHERE person_id =" + _id, con);

(Java)PreparedStatement command0 = con.prepareStatement("SELECT * FROM tbl_person WHERE person_id =" + _id);
 
Status
Not open for further replies.

Similar threads

Back
Top