读书人

如何运行不了呢

发布时间: 2012-05-22 18:18:54 作者: rapoo

怎么运行不了呢?
import java.sql.*;
public class SQLTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con;
String str;
Statement sta;
ResultSet re;
try{

//Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e){
System.out.println("ClassNotFoundException:"+e.getMessage());
}

try{
con= DriverManager.getConnection("jdbc:odbc:MySQLServer" ,"sa","");
System.out.println("连接已建立!");
sta =con.createStatement();

//建立成绩表
str=" create table chengji ( num int Not Null,name varchar(22),sex char(8),age int,score int)";
re = sta.executeQuery(str);
System.out.println("成绩表已建立!");

//插入5条记录
str="insert into chengji(num,name,sex,age,score) " +
"select 1,'崔伟','男',23,590 " +
"union " +
"select 2,'刘伟','女',25,345 " +
"union " +
"select 3,'景泽阳','男',26,456 " +
"union " +
"select 4,'张树兵','女',27,678 " +
"union " +
"select 5,'安进龙','男',25,564" ;

re=sta.executeQuery(str);
System.out.println("插入记录成功!");

sta.close();
con.close();
}
catch(SQLException ex){
System.err.println("SQLException:"+ex.getMessage());
}
}

}

运行结果:
连接已建立!
SQLException:No ResultSet was produced


[解决办法]
re=sta.executeQuery(str);
System.out.println("插入记录成功!");

==>

re=sta.executeUpdate(str);
System.out.println("插入记录成功!");
[解决办法]
try{
con= DriverManager.getConnection("jdbc:odbc:MySQLServer" ,"sa","");
System.out.println("连接已建立!");
sta =con.createStatement();

//建立成绩表
str=" create table chengji ( num int Not Null,name varchar(22),sex char(8),age int,score int)";
re = sta.executeQuery(str); System.out.println("成绩表已建立!");


你是创建表 是DDL 不能用ExecuteQuery(); 你直接用execute() 就行了

读书人网 >Eclipse开发

热点推荐