当前位置: 技术问答>java相关
sql查询的简单问题
来源: 互联网 发布时间:2015-09-05
本文导语: 我在执行以下语句时出错,不知道原因,请搞人指点: String driverName = "sun.jdbc.odbc.JdbcOdbcDriver"; String sourceURL = "jdbc:odbc:yuliao"; Class.forName (driverName); databaseConnection = DriverManager.getConnection(sourceURL);...
我在执行以下语句时出错,不知道原因,请搞人指点:
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String sourceURL = "jdbc:odbc:yuliao";
Class.forName (driverName);
databaseConnection = DriverManager.getConnection(sourceURL);
Statement myStatement = databaseConnection.createStatement();
String characters="上海"
ResultSet myResults = myStatement.executeQuery("SELECT frequency FROM yuliao WHERE keyword=characters");
出错提示:
SQL Exception:java.sql.SQLException:[Microsoft][ODBC SQL Server Driver][SQL Server]列名'characters'无效
另外,我使用的是sql2000
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String sourceURL = "jdbc:odbc:yuliao";
Class.forName (driverName);
databaseConnection = DriverManager.getConnection(sourceURL);
Statement myStatement = databaseConnection.createStatement();
String characters="上海"
ResultSet myResults = myStatement.executeQuery("SELECT frequency FROM yuliao WHERE keyword=characters");
出错提示:
SQL Exception:java.sql.SQLException:[Microsoft][ODBC SQL Server Driver][SQL Server]列名'characters'无效
另外,我使用的是sql2000
|
ResultSet myResults = myStatement.executeQuery("SELECT frequency FROM yuliao WHERE keyword= '"+characters"'");
加上单引号。。。。。。
加上单引号。。。。。。
|
这句应该这么写:
ResultSet myResults = myStatement.executeQuery("SELECT frequency FROM yuliao WHERE keyword = '" + characters + "'");
ResultSet myResults = myStatement.executeQuery("SELECT frequency FROM yuliao WHERE keyword = '" + characters + "'");
|
一样的,也要加上':
myStatement.executeQuery("INSERT into yuliao (keyword,frequency) values ('" + characters + "',1) ");
这是因为SQL中的字符串需用''来标识。而把characters从""中拿出,是因为它是变量,要加入到字符串中的是它的值。
myStatement.executeQuery("INSERT into yuliao (keyword,frequency) values ('" + characters + "',1) ");
这是因为SQL中的字符串需用''来标识。而把characters从""中拿出,是因为它是变量,要加入到字符串中的是它的值。
|
namowen(寒号不已)
写的正确。
写的正确。