关于java用在sql server中查询的总题

来源:百度知道 编辑:UC知道 时间:2024/06/23 07:54:20
本人用Java+Sql Server 做了个小系统,其中有个表中的主键是身份证号,大家都知道有的身份证号的最后一位是字母,当我用全数字的字符串去查询时能查出结果,但用数字加字母查询时却出现错误,查不出结果.
例如:select * from table where id = "430124198710234658";能查出结果
select * from table where id = "43012419871023465X";却出现错误,有数据库异常.
这是为什么?请高人指点,谢谢!
说明:我用的要JDBC.ODBC数据源.
public boolean search(String id)
{
//id="430124198610175610";时无错误,能正确查询
//id="43012419861017561X";时显示错误:[Microsoft][ODBC SQL Server Driver][SQL Server]第 1 行: 'x' 附近有语法错误。
boolean returns = false;//如果有查询结果,则返回true,否则返回false
try{
//加载JDBC-ODBC桥驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//连接ODBC数据源hospital_patient
Connection conn = DriverManager.getConnection("jdbc:odbc:hospital_patient");
Statement smt = conn.createStatement();
String sql = "select * from patient where id="+id;
ResultSet resul

把你的43012419871023465X 定义为一个变量
String str="43012419871023465X";
查询语句:
"select * from table where id = "+str;

数据库的id类型是int类型的,"43012419871023465X"这种写法是字符串,当然会报错误了

数据库设计的时候id的属性是int型,不是字符串。修改下id的属性看看

先不管你数据库中字段类型了 这样写:
String sql = "select * from patient where id='"+id+"'";

貌似你应该是ID写成INT了

没有。我是用输入框JTextField,用getText()方法将里面内容作为参数把ID传过来的。