com.microsoft.sqlserver.jdbc.SQLServerException: '1' 附近

J2EE 码拜 8年前 (2016-03-17) 1116次浏览
package com.wl.sm.framework.model.util;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DBUtil {
private List<Connection> list = null;
private Connection conn = null;
private Statement sta = null;
private ResultSet rs = null;
/*private void connection2DB() {
try {
Class.forName(“com.miccrosoft.sqlserver.jdbc.SQLServerDriver”);
this.conn = DriverManager.getConnection(“jdbc:sqlserver://127.0.0.1:1433;DatabaseName=schoolmis_new”, “sa”, “sa”);
System.out.println(“数据库连接成功!”);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
private Connection getConnection() {
Connection myconn = null;
try {
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
myconn = DriverManager.getConnection(“jdbc:sqlserver://127.0.0.1:1433;DatabaseName = schoolmis_new”,”sa”,”sa”);
System.out.println(“数据库连接成功!”);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return myconn;
}
public DBUtil() {
this.list = new ArrayList<Connection>();
for(int i = 0;i < 5; i++) {
this.list.add(this.getConnection());
}
this.conn = this.list.get(0);
}
public int update(String sql) {
int n = -1;
try {
this.sta = this.conn.createStatement();
n = this.sta.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}
public ResultSet query(String sql) {
try {
this.sta = this.conn.createStatement();
this.rs = this.sta.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.rs;
}
public void close() {
try {
if(this.rs != null) {
this.rs.close();
this.rs = null;
}
if(this.sta != null) {
this.sta.close();
this.sta = null;
}
if(this.conn != null) {
this.conn.close();
this.conn = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
错误提示:
select *from OrgTypewhere 1=1 and orgTypeId like %”11″%and orgTypeName like %”a”%and orgtypeMemo like %”b”%
数据库连接成功!
数据库连接成功!
数据库连接成功!
数据库连接成功!
数据库连接成功!
com.microsoft.sqlserver.jdbc.SQLServerException: “1” 附近有语法错误。
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.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:792)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:689)
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.SQLServerStatement.executeQuery(SQLServerStatement.java:616)
at com.wl.sm.framework.model.util.DBUtil.query(DBUtil.java:68)
at com.wl.sm.framework.model.dao.impl.IOrgTypeImpl.findByLike(IOrgTypeImpl.java:88)
at com.wl.sm.framework.model.dao.impl.IOrgTypeImplTest.main(IOrgTypeImplTest.java:19)
Exception in thread “main” java.lang.NullPointerException
at com.wl.sm.framework.model.dao.impl.IOrgTypeImpl.findByLike(IOrgTypeImpl.java:90)
at com.wl.sm.framework.model.dao.impl.IOrgTypeImplTest.main(IOrgTypeImplTest.java:19)
解决方案

10

很显然语句里边少空格啊

10

这么写:
select * from OrgType where orgTypeId like ‘%11%’ and orgTypeName like ‘%a%‘ and orgtypeMemo like ’%b%‘

10

引用:

这么写:
select * from OrgType where orgTypeId like ‘%11%’ and orgTypeName like ‘%a%‘ and orgtypeMemo like ’%b%‘

楼上正解

10

引用:

String select = “select *from OrgType “;
String where = “where “;
if(orgtype.getOrgTypeId() != null && orgtype.getOrgTypeId().length() > 0) {
where = where +”orgTypeId like %””+orgtype.getOrgTypeId()+””% “;
}
if(orgtype.getOrgTypeName() != null && orgtype.getOrgTypeName().length() > 0) {
where = where + “and orgTypeName like %””+orgtype.getOrgTypeName()+””% “;
}
if(orgtype.getOrgTypeMemo() != null && orgtype.getOrgTypeMemo().length() > 0) {
where = where + “and orgtypeMemo like %””+orgtype.getOrgTypeMemo()+””%”;
}
String sql = select + where;
System.out.println(sql);
这个sql是两个拼接在一起的

两个问题:
字符之间有必要的空格,例如表名和where,where各个条件之间;
另外,like的引号要括住百分号


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明com.microsoft.sqlserver.jdbc.SQLServerException: '1' 附近
喜欢 (0)
[1034331897@qq.com]
分享 (0)