日期:2014-05-20  浏览次数:20794 次

JDBC语法错误。。
import java.sql.*;
import java.util.*;
import java.io.*;

/**
 * Description:
 * <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author Yeeku.H.Lee kongyeeku@163.com
 * @version 1.0
 */
public class PreparedStatementTest
{
private String driver;
private String url;
private String user;
private String pass;
Connection conn;
Statement stmt;
PreparedStatement pstmt;

public void initParam(String paramFile)throws Exception
{
//使用Properties类来加载属性文件
Properties props = new Properties();
props.load(new FileInputStream(paramFile));
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
pass = props.getProperty("pass");
}

public void insertUseStatement()throws Exception
{int j=0;
long start = System.currentTimeMillis();
try
{
//使用Connection来创建一个Statment对象
stmt = conn.createStatement();
//需要使用100条SQL语句来插入100条记录
for (int i = 0; i < 100 ; i++ )
{
stmt.executeUpdate("insert into student_table values(j,'姓名"
+ i + "',1)");
j++;
}
System.out.println("使用Statement费时:" + (System.currentTimeMillis() - start));
}
//使用finally块来关闭数据库资源
finally
{
if (stmt != null)
{
stmt.close();
}
}
}

public void insertUsePrepare()throws Exception
{int j=0;
long start = System.currentTimeMillis();
try
{
//使用Connection来创建一个PreparedStatment对象
pstmt = conn.prepareStatement("insert into student_table values(?,?,1)");
//100次为PreparedStatement的参数设置,就可以插入100条记录
for (int i = 0; i < 100 ; i++ )
{
pstmt.setString(2 , "姓名" + i);
pstmt.setInt(1 ,j);
j++;
pstmt.executeUpdate();
}
System.out.println("使用PreparedStatement费时:" + (System.currentTimeMillis() - start));
}
//使用finally块来关闭数据库资源
finally
{
if (pstmt != null)
{
pstmt.close();
}
}

}
//定义打开连接的方法
public void getConn()throws Exception
{
if (conn == null)
{
//加载驱动
Class.forName(driver);
//获取数据库连接
conn = DriverManager.getConnection(url , user , pass);
}
}
//定义关闭连接的方法
public void closeConn()throws Exception
{
if (conn != null)
{
conn.close();
}
}

public static void main(String[] args) throws Exception
{
PreparedStatementTest pt = null;
try
{
pt = new PreparedStatementTest();
pt.initParam("sql2005.ini");
pt.getConn();
pt.insertUseStatement();
pt.insertUsePrepare();
}
finally
{
pt.closeConn();
}
}
}



Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: 在此上下文中不允许使用名称 "j"。有效表达式包括常量、常量表达式和变量(在某些上下文中)。不允许使用列名。

------解决方案--------------------
stmt.executeUpdate("insert into student_table values(j,'姓名"
+ i + "',1)");


改成

stmt.executeUpdate("insert into student_table values("+j+",'姓名"
+ i + "',1)");