为什么出现“将 varchar 值 's' 转换为数据类型为 int 的列时发生语法错误。”
想用JTree建一棵树,监听叶子节点的变化并显示出来,数据库(SQL Server2000)里节点node是varchar类型,一开始为方便用数字1,2、、、代替,一切正常,可是后来我改为中文后(甚至英文字母也是)就出错了,究竟什么原因啊?
=============///递归建树:
public void Creat(String pid, DefaultMutableTreeNode parent) {
try {
String sql = "select * from Test where ParentsID = " + pid;
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
node = new DefaultMutableTreeNode(rs.getString(3));
parent.add(node);
Creat(rs.getString( "ID "), node);
}
} catch (Exception e) {
e.printStackTrace();
}
}
========================///监听叶子节点:
CreatTree ct = new CreatTree();
tree = ct.getTree();
// 添加监视树节点的选择事件
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
leafShow(e);
}
});
==========================///响应监听:
private void leafShow(TreeSelectionEvent e) {
path = e.getNewLeadSelectionPath();
node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (node.isLeaf()) {
String sql = "select node,oldItem,newItem from Test where node = "
+ node.toString();
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
rs.next();
label_2.setText(rs.getString( "node ") + rs.getString( "oldItem "));
comboBox.removeAllItems();
comboBox.addItem(rs.getString( "oldItem "));
comboBox.addItem(rs.getString( "newItem "));
} catch (SQLException ex) {
ex.printStackTrace();//System.out.println( "leafShow ");//
}
}
}
===========================////出错:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]将 varchar 值 's ' 转换为数据类型为 int 的列时发生语法错误。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.getRow(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplResultSet.positionCursor(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.next(Unknown Source)
at change.Change.leafShow(Change.java:146)
at change.Change.access$2(Change.java:137)
at change.Change$3.valueChanged(Change.java:128)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:287)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:170)
at javax.swing.JTree.setSelectionPath(JTree.java:1174)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2296)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelectionImpl(BasicTreeUI.java:3509)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3484)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3465)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5485)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1778)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
[解决办法]
String sql = "select * from Test where ParentsID = " + pid;
--> 改为
String sql = "select * from Test where ParentsID = ' " + pid + " ' ";