我编写了一个向数据库中插入数据的类,编译时没有出错,也就是说语法上没有错,但运行时就报错了:
抛出的异常如下:java.sql.SQLException:[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]
在此上下文中不支持使用‘林时’。此处只允许使用常量、表达式或变量。不允许使用列明。后面时一大堆的异常。
我的AspSql类源代码如下
import java.sql.*;
//import test.ShowTime;/**
* @author huangqin
*
*/
public class AspSql {
private String number;
private String name;
private String subject;
private char sex;
private String birthday;
private int grade;
private String context;
private final int maxSize=20;
public AspSql() throws InstantiationException,
IllegalAccessException, ClassNotFoundException{
number=new String("051118");
name=new String("林时");
subject=new String("计算机");
sex='男';
birthday=new String("1988-05-08");
grade=80;
context=new String("他是一个好学生");
String userName="sa";
//password
String userPassword="";
//The database name
String dbName="xscj";
//connect the string
String url="jdbc:microsoft:sqlserver://localhost:1433;"+
dbName+"?useUnicode=true&characterEncoding=GBK";
Statement stmt=null;
ResultSet result=null;
Connection conn=null;
String sql=null;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn=DriverManager.getConnection(url,userName,userPassword);
}
catch(SQLException e)
{
System.out.print(e.toString());
}
try{
stmt=conn.createStatement();
}catch(SQLException e){
e.printStackTrace();
}
try{
//利用循环向数据库中插入数据
for(int i=0;i<maxSize;i++)
{
sql="insert into xs values("+number+","+name+","+birthday+","+sex+","+
subject+","+grade+","+context+")";
result=stmt.executeQuery(sql);
}
}catch(SQLException e){
e.printStackTrace();
}
try{
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
if(result!=null)
result.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String args[])
throws InstantiationException, IllegalAccessException, ClassNotFoundException{
AspSql aspsql=new AspSql();
}
}(还有创建的数据库中的表代码)所在的数据库时XSCJ:
create table xs(
xh char(8) not null primary key,
xm char(8) ,
xb char(1),
zy char(12),
cssj datetime ,
zxf int ,
bz varchar,);
go
抛出的异常如下:java.sql.SQLException:[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]
在此上下文中不支持使用‘林时’。此处只允许使用常量、表达式或变量。不允许使用列明。后面时一大堆的异常。
我的AspSql类源代码如下
import java.sql.*;
//import test.ShowTime;/**
* @author huangqin
*
*/
public class AspSql {
private String number;
private String name;
private String subject;
private char sex;
private String birthday;
private int grade;
private String context;
private final int maxSize=20;
public AspSql() throws InstantiationException,
IllegalAccessException, ClassNotFoundException{
number=new String("051118");
name=new String("林时");
subject=new String("计算机");
sex='男';
birthday=new String("1988-05-08");
grade=80;
context=new String("他是一个好学生");
String userName="sa";
//password
String userPassword="";
//The database name
String dbName="xscj";
//connect the string
String url="jdbc:microsoft:sqlserver://localhost:1433;"+
dbName+"?useUnicode=true&characterEncoding=GBK";
Statement stmt=null;
ResultSet result=null;
Connection conn=null;
String sql=null;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn=DriverManager.getConnection(url,userName,userPassword);
}
catch(SQLException e)
{
System.out.print(e.toString());
}
try{
stmt=conn.createStatement();
}catch(SQLException e){
e.printStackTrace();
}
try{
//利用循环向数据库中插入数据
for(int i=0;i<maxSize;i++)
{
sql="insert into xs values("+number+","+name+","+birthday+","+sex+","+
subject+","+grade+","+context+")";
result=stmt.executeQuery(sql);
}
}catch(SQLException e){
e.printStackTrace();
}
try{
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
if(result!=null)
result.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String args[])
throws InstantiationException, IllegalAccessException, ClassNotFoundException{
AspSql aspsql=new AspSql();
}
}(还有创建的数据库中的表代码)所在的数据库时XSCJ:
create table xs(
xh char(8) not null primary key,
xm char(8) ,
xb char(1),
zy char(12),
cssj datetime ,
zxf int ,
bz varchar,);
go
然后把"?useUnicode=true&characterEncoding=GBK"去掉,使用平台默认字符集看看是否还有问题
比如:
jdbc:microsoft:sqlserver://localhost:1433/splan 这样试下
比如,你的句子还原出来是
insert into xs values(051118,林时,1988-05-08,男,计算机,80,他是一个好学生)
但是你数据库里要求char应该是'林时'....前边的纯数字要不要''对char不会有影响后面你可能会碰到提示datetime/出生日期的问题,因为数据库的datetime类型似乎不是你的'1988-05-08'这个样子的
jdbc.url=jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=textSQL
jdbc.username=sa
jdbc.password=sa
dbName+"?useUnicode=true&characterEncoding=GBK";
改成
String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName="+
dbName+"?useUnicode=true&characterEncoding=GBK";
试一下!
insert into xs values('051117','林时','计算机','男','1985-04-05','80','的师傅说的')
go
能成功插入数据
subject+","+grade+","+context+")";
// 有问题,改成
sql="insert into xs values('"+number+"','"+name+"','"+birthday+"','"+sex+"','"+
subject+"','"+grade+"','"+context+"')";
// 应该就没问题了!
//利用循环向数据库中插入数据
for(int i=0;i <maxSize;i++)
{
sql="insert into xs values("+number+","+name+","+birthday+","+sex+","+
subject+","+grade+","+context+")";
result=stmt.executeQuery(sql);
} 你是初学者吗?你自己看看.你给的是什么方法....这是查询的方法....
result=stmt.executeQuery(sql); 应该是 result=stmt.executeUpdate(sql);
//利用循环向数据库中插入数据
for(int i=0;i <maxSize;i++)
{
sql="insert into xs values("+number+","+name+","+birthday+","+sex+","+
subject+","+grade+","+context+")";
result=stmt.executeQuery(sql);
} 你是初学者吗?你自己看看.你给的是什么方法....这是查询的方法....
result=stmt.executeQuery(sql); 应该是 result=stmt.executeUpdate(sql);
难道还要管理工具中建立数据源?可我用的不是ODBC--JDBC呀。
对头,少很多烦恼,何必拼sql呢,多累
subject+","+grade+","+context+")"; 改为
sql="insert into xs values('"+number+"','"+name+"','"+birthday+"','"+sex+"','"+
subject+"','"+grade+"','"+context+"')";
拼接时少了''
这问题都有:
sql="insert into xs values("+number+","+name+","+birthday+","+sex+","+
subject+","+grade+","+context+")";
// 有问题,改成
sql="insert into xs values('"+number+"','"+name+"','"+birthday+"','"+sex+"','"+
subject+"','"+grade+"','"+context+"')";
// 应该就没问题了! try{
//利用循环向数据库中插入数据
for(int i=0;i <maxSize;i++)
{
sql="insert into xs values("+number+","+name+","+birthday+","+sex+","+
subject+","+grade+","+context+")";
result=stmt.executeQuery(sql);
} 你自己看看.你给的是什么方法....这是查询的方法....
result=stmt.executeQuery(sql); 应该是 stmt.executeUpdate(sql);