package shujuku;
import java.sql.*;
public class Jdbcodbc {
public static void main(String [] args){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=TEST", "sa", "123456");
//Statement stmt=con.createStatement();
String sql="insert into userTable(id,username,password) values(?,?,?,)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setInt(1, 33);
pstmt.setString(2," zhang");
pstmt.setString(3, "345678");
int i=0;
i=pstmt.executeUpdate();
if(i!=0){
System.out.println("注册成功");
//逐个访问数据库成员
/*ResultSet rs=stmt.executeQuery("select * from userTable");
System.out.println("账号 姓名 密码");
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+"");
} */
//rs.close();
//stmt.close();
}
pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
com.microsoft.sqlserver.jdbc.SQLServerException: ')' 附近有语法错误。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)
at shujuku.Jdbcodbc.main(Jdbcodbc.java:15)
------解决思路----------------------
values(?,?,?,)
去掉最后一个逗号, 变成 ::::
values(?,?,?) 。。。。改之