当前位置: 技术问答>java相关
JDBC连接MYSQL,可以进行插入操作,但查询却不能进行
来源: 互联网 发布时间:2017-03-29
本文导语: 用的MYSQL版本是3.23.52, 驱动装的是mysql-connector-java-3.0.2-beta-bin.jar。 连接应该是没有问题的,因为用INSERT可以写入数据库。 但用 String queryString = " SELECT ID,Name FROM test"却不能检索数据表。 具体代码如下: public ...
用的MYSQL版本是3.23.52,
驱动装的是mysql-connector-java-3.0.2-beta-bin.jar。
连接应该是没有问题的,因为用INSERT可以写入数据库。
但用 String queryString = " SELECT ID,Name FROM test"却不能检索数据表。
具体代码如下:
public class JDBCTest {
private static final String DRIVER_NAME = "org.gjt.mm.mysql.Driver";
private static final String DB_URL="jdbc:mysql://localhost/kelvin?user=root&password=666666";
private static final String QUERY= "SELECT ID,Name FROM test";
private static final String insertSql= "insert into test values(133,'aaa')";
public static void main(String[] args) {
try {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException ex) {ex.printStackTrace();
}
Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt = conn.createStatement();
stmt.execute(insertSql);
ResultSet rslt = stmt.executeQuery(QUERY);
while (rslt.next() ) {
String name = rslt.getString("Name");
int id = rslt.getInt("ID");
System.out.println(id+"1111 " +name);
}
rslt.close();
stmt.close();
conn.close();
System.out.println("Success ! Connected to database");
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
运行报错:
java.sql.SQLException: Column 'Name' not found.
at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:2862)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:1990)
at com.kelvin.security.installtest.JDBCTest.main(JDBCTest.java:50)
若是用 (50) String name = rslt.getString(2);
(51) int id = rslt.getInt(1);
则出现:
java.sql.SQLException: Invalid value for getInt() - 'qpp'
at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:1323)
at com.kelvin.security.installtest.JDBCTest.main(JDBCTest.java:51)
我很奇怪为什么写数据库可以,而读却不行?insertSql= "insert into test values(133,'aaa')"这条语句可以执行。select时也检索到了记录条数,但我就是得不到响应的列。
驱动装的是mysql-connector-java-3.0.2-beta-bin.jar。
连接应该是没有问题的,因为用INSERT可以写入数据库。
但用 String queryString = " SELECT ID,Name FROM test"却不能检索数据表。
具体代码如下:
public class JDBCTest {
private static final String DRIVER_NAME = "org.gjt.mm.mysql.Driver";
private static final String DB_URL="jdbc:mysql://localhost/kelvin?user=root&password=666666";
private static final String QUERY= "SELECT ID,Name FROM test";
private static final String insertSql= "insert into test values(133,'aaa')";
public static void main(String[] args) {
try {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException ex) {ex.printStackTrace();
}
Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt = conn.createStatement();
stmt.execute(insertSql);
ResultSet rslt = stmt.executeQuery(QUERY);
while (rslt.next() ) {
String name = rslt.getString("Name");
int id = rslt.getInt("ID");
System.out.println(id+"1111 " +name);
}
rslt.close();
stmt.close();
conn.close();
System.out.println("Success ! Connected to database");
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
运行报错:
java.sql.SQLException: Column 'Name' not found.
at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:2862)
at com.mysql.jdbc.ResultSet.getString(ResultSet.java:1990)
at com.kelvin.security.installtest.JDBCTest.main(JDBCTest.java:50)
若是用 (50) String name = rslt.getString(2);
(51) int id = rslt.getInt(1);
则出现:
java.sql.SQLException: Invalid value for getInt() - 'qpp'
at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:1323)
at com.kelvin.security.installtest.JDBCTest.main(JDBCTest.java:51)
我很奇怪为什么写数据库可以,而读却不行?insertSql= "insert into test values(133,'aaa')"这条语句可以执行。select时也检索到了记录条数,但我就是得不到响应的列。
|
如果字段已经存在,则把是数据库关键字的"Name"用[]括起来,如上
|
大哥,注意一下大小写!!
Column 'Name' not found.明白告诉你没有这个列。
能把你的数据库表结构贴上来吗??
Column 'Name' not found.明白告诉你没有这个列。
能把你的数据库表结构贴上来吗??
|
Statement stmt = conn.createStatement();
有问题:
因为创建默认语句句柄时,所产生结果集仅支持向前滚动,所以呢你的创建这样的语句句柄(支持可滚动(两个方向)和可更新的结果集)
改为:Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
你试试看!!!!!
有问题:
因为创建默认语句句柄时,所产生结果集仅支持向前滚动,所以呢你的创建这样的语句句柄(支持可滚动(两个方向)和可更新的结果集)
改为:Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
你试试看!!!!!
|
不妨这样试一下:
你在创建两个语句句柄:一个插入行的语句句柄,一个是检索结果的语句句柄
代码如下:
public class JDBCTest {
private static final String DRIVER_NAME = "org.gjt.mm.mysql.Driver";
private static final String DB_URL="jdbc:mysql://localhost/kelvin?user=root&password=666666";
private static final String QUERY= "SELECT ID,Name FROM test";
private static final String insertSql= "insert into test values(133,'aaa')";
public static void main(String[] args) {
try {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException ex) {ex.printStackTrace();
}
Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt_insert = conn.createStatement();
stmt_insert.execute(insertSql);
Statement stmt_query = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rslt = stmt_query.executeQuery(QUERY);
while (rslt.next() ) {
String name = rslt.getString("Name");
int id = rslt.getInt("ID");
System.out.println(id+"1111 " +name);
}
rslt.close();
stmt_insert.close();
stm_qurey.close();
conn.close();
System.out.println("Success ! Connected to database");
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
你在创建两个语句句柄:一个插入行的语句句柄,一个是检索结果的语句句柄
代码如下:
public class JDBCTest {
private static final String DRIVER_NAME = "org.gjt.mm.mysql.Driver";
private static final String DB_URL="jdbc:mysql://localhost/kelvin?user=root&password=666666";
private static final String QUERY= "SELECT ID,Name FROM test";
private static final String insertSql= "insert into test values(133,'aaa')";
public static void main(String[] args) {
try {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException ex) {ex.printStackTrace();
}
Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt_insert = conn.createStatement();
stmt_insert.execute(insertSql);
Statement stmt_query = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rslt = stmt_query.executeQuery(QUERY);
while (rslt.next() ) {
String name = rslt.getString("Name");
int id = rslt.getInt("ID");
System.out.println(id+"1111 " +name);
}
rslt.close();
stmt_insert.close();
stm_qurey.close();
conn.close();
System.out.println("Success ! Connected to database");
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}