是什么让这个SQL在JDBC中失败? [英] What makes this sql failure in JDBC?

查看:61
本文介绍了是什么让这个SQL在JDBC中失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全相同的sql语句在MySQL Workbench中工作正常但在Java程序中失败。我所做的就是在Java中执行此语句之前设置一个断点,并将相同的语句(select * from administrator where AdministratorID ='Admin')从Java粘贴到Workbench,其中显示了ONE记录。我用try-catch包围了调用,结果成功调用但没有返回任何内容(resultset.getRow()返回0)。以下是执行查询的Java代码:

The very same sql statement works fine in MySQL Workbench but fails in Java program. All I've done was setting a breakpoint before the execution of this statement in Java and paste the same statement ("select * from administrator where AdministratorID = 'Admin'") from Java to the Workbench, where showed ONE record. I surrounded the invoking with try-catch, it turned out successfully called but returned nothing (resultset.getRow() returns 0). Here is the Java code that executes the query:

public ResultSet executeQuery(String tableName, String condition) {
		String sql = "select * from " + tableName + " where " + condition;
		ResultSet rs = null;
		Connection conn = null;
		try {
			conn = getConn();
			Statement statement = conn.createStatement();
			rs = statement.executeQuery(sql);
			statement.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}



和getConn()函数:


And the getConn() function:

public Connection getConn() {
		Connection conn = null;
		try {
			Class.forName(name); // 指定连接类型
			String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName;
			conn = DriverManager.getConnection(url, user, password); // 获取连接
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}



其中name =com.mysql.jdbc.Driver,host =127.0.0.1,port =3306。正确提供了dbName,用户和密码。

任何人都可以帮忙告诉我出了什么问题?


where name = "com.mysql.jdbc.Driver", host = "127.0.0.1", port = "3306". dbName, user and password were provided correctly.
Can anyone help tell me what went wrong?

推荐答案

在任何事情发生之前关闭连接读。只是为了测试,尝试在 executeQuery rs = statement.executeQuery(sql); 之后读取结果集中的数据c>方法。



只是关于良好编程风格的建议。没有你的方法 getConn 如果创建连接失败,则返回 null 。简单地说明例外情况。这使它更清楚地说明它何时以及为什么它从它被调用的地方失败。



当然......谨防sql注入!



祝你好运!
You close the connection before anything was read. Just for test, try reading data from the resultset right after the line rs = statement.executeQuery(sql); in your executeQuery method.

Just an advice on good programming style. Don't have your method getConn return null if creating a connection failed. Simply retrow the exception. This makes it so much more clear when and why it fails from where it is called.

And of course... beware of sql injection!

Good luck!


结果集游标位于第一行之前。通过使用next(),您可以将光标移动到下一行。发布最后一行(即 - 如果光标位于最后一行之后)next()将返回false

更多细节可以在这里找到:

https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html [< a href =https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html\"target =_ blanktitle =New Window> ^ ]
The resultset cursor is positioned before the first row. By using next() you move the cursor to next row. Post the final row (ie- if the cursor is positioned after last row) next() will return false
More details can be found here :
https://docs.oracle.com/javase/tutorial/jdbc/basics/retrieving.html[^]


这篇关于是什么让这个SQL在JDBC中失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆