gpt4 book ai didi

java - 如何使用准备好的语句

转载 作者:行者123 更新时间:2023-12-02 13:00:23 26 4
gpt4 key购买 nike

有人建议使用准备好的语句,但我不知道如何使用它。我必须在代码中进行哪些更改?

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("\n Driver loaded");

Connection con = DriverManager.getConnection("jdbc:odbc:wanisamajDB");

Statement stmt = con.createStatement();
System.out.println("statement is created");

// System.out.println(Integer.parseInt(cbregn.getSelectedItem().toString()));

String qry = " UPDATE Registration1 SET RegistrationNo = '"+cbregn.getSelectedItem()+"',SeniorPerson = '"+cbnm.getSelectedItem()+"', NativePlace = '"+tfplace.getText()+"',Kul = '"+tfkul.getText()+"', Gotra = '"+tfgotra.getText()+"' ,KulSwami = '"+tfswami.getText()+"', ResidensialAddress = '"+taraddr.getText()+"' , PinCode = '"+tfpcd.getText()+"', STDcode = '"+tfstdcode.getText()+"',TelephoneNo = '"+tftele.getText()+"', MobileNo = '"+tfmno.getText()+"', Email = '"+tfemail.getText()+"',Website ='"+tfweb.getText()+"',Education ='"+tfedu.getText()+"',Branch ='"+tfbrch.getText()+"',BloodGroup ='"+cbbldgrp.getSelectedItem()+"' where SeniorPerson='" +cbnm.getSelectedItem().toString()+"'" ;

stmt.executeUpdate(qry);

JOptionPane.showMessageDialog(null,"RECORD IS UPDATED SUCCESSFULLY ");
System.out.println("QUERY");

// cbregn.setEditable(false);
cbnm.setEditable(false);
tfplace.setEditable(false);
tfkul.setEditable(false);
tfgotra.setEditable(false);
tfswami.setEditable(false);
taraddr.setEditable(false);
tfpcd.setEditable(false);
tfstdcode.setEditable(false);
tftele.setEditable(false);
tfmno.setEditable(false);
tfemail.setEditable(false);
tfweb.setEditable(false);
tfedu.setEditable(false);
tfbrch.setEditable(false);
cbbldgrp.setEditable(false);
con.close();
stmt.close();
}
// catch(SQLException eM)
// {
// JOptionPane.showMessageDialog(null,"RECORD IS NOT FOUND ");
// }
catch(Exception et)
{
et.printStackTrace();
// System.out.println("error:"+et.getMessage());
}

最佳答案

参见example

准备好的语句可以通过将 SQL 逻辑与所提供的数据分开来帮助提高安全性。这种逻辑和数据的分离有助于防止一种非常常见的漏洞,称为 SQL 注入(inject)攻击。通常,当您处理即席查询时,在处理从用户收到的数据时需要非常小心。这需要使用转义所有必要的麻烦字符的函数,例如单引号、双引号和反斜杠字符。在处理准备好的语句时这是不必要的。数据的分离允许 MySQL 自动考虑这些字符,并且不需要使用任何特殊函数对它们进行转义。

关于java - 如何使用准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5591271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com