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() 就行了