我知道PreparedStatements避免/阻止SQL注入.它是如何做到的?使用PreparedStatements构造的最终表单查询是字符串还是其他?
如果将PreparedStatement与没有任何池的单个公共连接一起使用,我是否可以为每个dml/sql操作重新创建一个实例来保证预处理语句的强大功能?
我的意思是:
for (int i=0; i<1000; i++) {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, someValue);
preparedStatement.executeQuery();
preparedStatement.close();
}
Run Code Online (Sandbox Code Playgroud)
代替:
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int i=0; i<1000; i++) {
preparedStatement.clearParameters();
preparedStatement.setObject(1, someValue);
preparedStatement.executeQuery();
}
preparedStatement.close();
Run Code Online (Sandbox Code Playgroud)
我的问题是因为我想把这段代码放到多线程环境中,你能给我一些建议吗?谢谢
我知道使用的优点PreparedStatement,这些都是
但我想知道我们何时使用它而不是Statement?
就在上周,我正在做一些PHP的东西.我做了一些小的解决方案来防止SQL注入.PHP一直是我的男人,它有3个解决方案可供使用(可能更多).一种是使用stripslashes()函数启用"魔术查询" .另一个(推荐)是使用mysql_real_escape_string()功能.那个简单,我的问题就解决了.但是,当谈到JSP时,事情似乎并不那么简单.我搜索并没有找到任何内置函数来删除斜杠或做那些事情(我相信这样的功能可以使用基本的JAVA函数实现,但......).
请帮我保护我的数据库.我听说过PreparedStatement,但真的无法理解我的脑袋?(我觉得新手的真正含义).
何时使用语句而不是预备语句.我假设语句用于没有参数的查询,但为什么不使用预准备语句?对于没有参数的查询,哪一个更快.
我想知道有什么区别,何时使用Statement,PreparedStatement和CallableStatement.
使用这些方法的最佳实践和典型方案是什么?
我在互联网上阅读了它,但我无法得到某些查询的答案。
一季度。据说在执行PreparedStatement时,DBMS可以直接运行PreparedStatement SQL语句,无需先编译。我的问题是当preparedStetement.execute() 被调用时会发生什么?(我的理解:没有任何参数的sql被发送到DBMS,由DBMS编译和缓存以备将来使用。然后发送参数&DBMS用占位符替换它们&执行。)
Q2。如果下次我使用相同的 sql 执行 PreparedStetement.execute() 那么会发生什么?(我的理解:DBMS 将 sql 与之前的 sql 进行比较,如果匹配则采用编译后的 sql,替换参数并执行。)
Q3。如果我偶尔调用数据库,那么准备好的语句不会帮助我提高性能,因为在此期间数据库缓存将被清除。所以每次都会编译sql。对?
我在我们的代码库上运行了findbugs,它指出还有两个语句仍然需要关闭.在我们运行的代码的这一部分中:
preparedStatement = connection.prepareStatement(query);
Run Code Online (Sandbox Code Playgroud)
对于3个不同的查询,重用prepareStatement.在finally块中,我们关闭资源:
finally{
try{
if (resultSet != null)
resultSet.close();
} catch (Exception e) {
exceptionHandler.ignore(e);
}
try {
if (preparedStatement != null)
preparedStatement.close();
} catch(Exception e) {
exceptionHandler.ignore(e);
}
Run Code Online (Sandbox Code Playgroud)
语句是否应在下一个connection.prepareStatement(query)之前关闭; 或者这个发现者是否谨慎?
我有一个应用程序,我正在连接到MySQL数据库.它在半夜失去连接,然后喷出null连接,JDBC在X秒内没有收到消息.
getConnection()在我做任何需要与SQL服务器通信的事情之前,我打电话.
这是我的getConnection()方法:
private Connection getConnection() {
try {
if (connection != null) {
if (connection.isClosed() || !connection.isValid(10000)) {
this.initializeRamsesConnection();
}
} else {
this.initializeRamsesConnection();
}
} catch (Exception e) {
debug("Connection failed: " + e);
}
return connection;
}
Run Code Online (Sandbox Code Playgroud)
在initializeRamsesConnection()方法中,我将密码等信息放入字符串中,然后以标准JDBC方式创建连接.
然后我称这个方法:
private Connection getConnectionFromConnectionString() {
Connection con = null;
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver);//jdbc sorcery
//if there is no connection string
if (getConnectionString() == null) {
HMIDatabaseAdapter.debug("No connection string");
} …Run Code Online (Sandbox Code Playgroud)