在做数据库课设,将java与sqlserver2012连接,运行时出现这样的错误
连接数据库成功
com.microsoft.sqlserver.jdbc.SQLServerException: 关键字 'select' 附近有语法错误。
不知道哪里有问题,这个错误是显示连接有问题吗
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1655)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:885)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:778)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:166)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServerStatement.java:703)
at com.yjdb.LoginFrame.actionPerformed(LoginFrame.java:97)
连接的代码如下,
package com.yjdb;
import java.sql.*;
public class ConnectDB {
/**
* @param args
*/
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=yjdb";
static String userName = "sa";
static String userPassword = "123456";
private Connection con;
public Statement stmt;
public Statement Connect(){
try{
Class.forName(driverName);
con = DriverManager.getConnection(url,userName,userPassword);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.println("连接数据库成功");
}
catch(Exception e){
e.printStackTrace();
}
return stmt;
}
}
这个是登陆界面的部分代码
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
String customerID = textField.getText();
String customerPassword = new String(passwordField.getPassword()).trim();
try{
String sql = "select * from [Customer] where CID='"+customerID+"'";//执行的sql语句,在数据库里查找管理员ID
stmt=new ConnectDB().Connect();
rs=stmt.executeQuery(sql);
if(rs.next())
{
if(customerPassword.equals(rs.getString("CPW").trim()))
{
String sql1 = "Create table NowCustomer ( select CID from Customer where CID='"+customerID+"' )";
Statement stmt1 = new ConnectDB().Connect();
stmt1.executeUpdate(sql1);
JOptionPane.showMessageDialog(null,"欢迎进入邮局订报管理系统","登录成功!",JOptionPane.INFORMATION_MESSAGE);
dispose();
new CustomerMainFrame(customerID);
}
else{
JOptionPane.showMessageDialog(null,"对不起,密码错误,请重新输入","登陆失败!",JOptionPane.ERROR_MESSAGE);
}
}
else{
JOptionPane.showMessageDialog(null,"用户ID不存在,请重新输入","登陆失败!",JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e1){
e1.printStackTrace();
}
}