📄 www_jieesoft_com 主题回顾 - jtree和数据库的一点小bug,请指教,谢谢.htm
字号:
<BR><BR>//treeModel.insertNodeInto(newNode,root,root.getChildCount());
<BR>int flag=1; <BR>try <BR>{ <BR>String query2="SELECT
nativeplaceName FROM nativeplace";
<BR>rst2=state2.executeQuery(query2); <BR>while(rst2.next()) <BR>{
<BR>String results=rst2.getString(1); <BR>if(results.equals(s))
<BR>{ <BR>flag=0; <BR>} <BR>} <BR><BR>if(flag==0) <BR>{
<BR>JOptionPane.showMessageDialog(addNativeplace.this,
<BR>"对不起,你输入的更改籍贯名称已经存在,请重新输入",
<BR>"籍贯输入错误",JOptionPane.ERROR_MESSAGE); <BR>} <BR><BR>if(flag==1)
<BR>{ <BR>String query="UPDATE nativeplace SET
nativeplaceName='"+s+"' WHERE nativeplaceName='"+parentNode+"'";
<BR>state1.executeUpdate(query);
<BR><BR>JOptionPane.showMessageDialog(addNativeplace.this,
<BR>"成功修改籍贯名称,请继续操作!","籍贯修改成功",
<BR>JOptionPane.INFORMATION_MESSAGE);
<BR>parentNode.setUserObject(s); <BR>} <BR><BR><BR>}
<BR>catch(SQLException sqle) <BR>{ <BR>sqle.printStackTrace(); <BR>}
<BR>} <BR>} <BR><BR>if(e.getSource()==deleteButton) <BR>{
<BR>TreePath treepath=tree.getSelectionPath();
<BR>DefaultMutableTreeNode
parentNode=(DefaultMutableTreeNode)(treepath.getLastPathComponent());
<BR>int result= JOptionPane.showConfirmDialog(addNativeplace.this,
<BR>"删除籍贯后该籍贯将不存在,是否确定要删除所选籍贯?", <BR>"
是否删除籍贯",JOptionPane.YES_NO_OPTION); <BR>TreeNode
parent1Node=(TreeNode)parentNode.getParent();
<BR><BR>if(treepath!=null) <BR>{ <BR>if(parent1Node!=null) <BR>{
<BR>if(result==JOptionPane.YES_OPTION) <BR>{ <BR>try <BR>{
<BR>String query="DELETE FROM nativeplace WHERE
nativeplaceName='"+parentNode+"'"; <BR>state1.executeUpdate(query);
<BR>} <BR>catch(SQLException sqle) <BR>{ <BR>sqle.printStackTrace();
<BR>} <BR>treeModel.removeNodeFromParent(parentNode); <BR>} <BR>}
<BR>} <BR><BR>} <BR>tree.updateUI();
<BR><BR>if(e.getSource()==exitButton) <BR>{ <BR><BR>dispose(); <BR>}
<BR><BR>} <BR><BR><BR>public void valueChanged(TreeSelectionEvent e)
<BR>{ <BR>TreePath path = e.getNewLeadSelectionPath();
<BR>if(path==null) <BR>return; <BR><BR>int
nodenumber=path.getPathCount(); <BR>if(path!=null) <BR>{
<BR>if(nodenumber==2) <BR>{ <BR>updateButton.setEnabled(true);
<BR>deleteButton.setEnabled(true); <BR>addButton.setEnabled(false);
<BR><BR>} <BR>else <BR>{ <BR>updateButton.setEnabled(false);
<BR>deleteButton.setEnabled(false); <BR>addButton.setEnabled(true);
<BR>} <BR><BR>} <BR><BR><BR><BR>} <BR><BR><BR><BR>public void
treeNodesChanged(TreeModelEvent e) <BR>{ <BR>TreePath
treePath=e.getTreePath(); <BR>DefaultMutableTreeNode
node=(DefaultMutableTreeNode)treePath.getLastPathComponent();
<BR>try <BR>{ <BR>int[] index=e.getChildIndices();
<BR>node=(DefaultMutableTreeNode)node.getChildAt(index[0]); <BR>}
<BR>catch(NullPointerException exc) <BR>{ <BR>exc.printStackTrace();
<BR>} <BR><BR><BR>} <BR>public void treeNodesInserted(TreeModelEvent
e) <BR>{ <BR><BR>} <BR>public void treeNodesRemoved(TreeModelEvent
e) <BR>{ <BR><BR>} <BR>public void
treeStructureChanged(TreeModelEvent e) <BR>{ <BR><BR>}
<BR><BR><BR>class MouseHandle extends MouseAdapter <BR>{ <BR>public
void mousePressed(MouseEvent e) <BR>{ <BR><BR>JTree
tree=(JTree)e.getSource(); <BR>int
rowLocation=tree.getRowForLocation(e.getX(),e.getY()); <BR>TreePath
treePath=tree.getSelectionPath(); <BR><BR>if(treePath!=null) <BR>{
<BR>TreeNode treenode=(TreeNode)treePath.getLastPathComponent();
<BR>} <BR><BR>} <BR>} <BR><BR><BR>}</SPAN></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=spaceRow colSpan=2 height=1><IMG height=1 alt=""
src="www_jieesoft_com 主题回顾 - JTree和数据库的一点小bug,请指教,谢谢.files/spacer.gif"
width=1></TD></TR>
<TR>
<TD class=row2 vAlign=top align=left width="22%"><SPAN class=name><A
name=""></A><B>一等奖</B></SPAN></TD>
<TD class=row2 vAlign=top height=28>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD width="100%"><IMG title=文章 height=9 alt=文章
src="www_jieesoft_com 主题回顾 - JTree和数据库的一点小bug,请指教,谢谢.files/icon_minipost.gif"
width=12 border=0><SPAN class=postdetails>时间: 周日 5 02, 2004 11:02
上午<SPAN class=gen> </SPAN> 标题:
JTree和数据库的一点小bug,请指教,谢谢</SPAN></TD></TR>
<TR>
<TD colSpan=2>
<HR>
</TD></TR>
<TR>
<TD colSpan=2><SPAN class=postbody><IMG
src="www_jieesoft_com 主题回顾 - JTree和数据库的一点小bug,请指教,谢谢.files/bbs.chinajavaworld[1]"
border=0>
<BR><BR><BR>我写了一个可是实现添加、修改和删除籍贯的程序,是和数据库连起来的,本数据库用access实现,但在测试的过程中出现一点小bug
。
<BR>1、修改籍贯的时候不能动态的刷新jtree上的籍贯,比如我修改“湖南”为“香港”,在数据库上实现了修改,但在jtree上还是“湖南”,请问怎么改进?
<BR>2、修改籍贯只能一次修改,就是说当我修改“湖南”为“香港”的时候,这个时候数据库里是“香港”,但在jtree上还是“湖南”,这个时候再点击“湖南”(实际数据是“香港”)改为“澳门”就没有反应了,连数据库里都实现不了更改,还是“香港”,请问怎么改进?
<BR>3、当我进行删除操作的时候,比如删除“福建”,可以jtree和数据库里都可以实现删除,但是报如图“找不到指针的错误”,请问怎么解决这个问题,谢谢。
<BR>谢谢 <BR>万分谢谢 <BR>欢迎指正批评。 <BR><BR><BR><BR>import java.awt.*;
<BR>import javax.swing.*; <BR>import java.sql.*; <BR>import
java.awt.event.*; <BR>import javax.swing.event.*; <BR>import
javax.swing.text.*; <BR>import javax.swing.tree.*; <BR><BR>public
class addNativeplace extends JFrame implements ActionListener,
<BR>TreeModelListener,TreeSelectionListener <BR>{ <BR>public static
void main(String args[]) <BR>{ <BR>new addNativeplace(); <BR>}
<BR><BR><BR>private JButton
addButton,updateButton,deleteButton,exitButton; <BR>private
Connection con1; <BR>private Statement state1,state2; <BR>private
ResultSet rst1,rst2; <BR>private JTree tree; <BR>private
DefaultTreeModel treeModel=null; <BR>private DefaultMutableTreeNode
root; <BR>private JScrollPane scroller1; <BR><BR>public
addNativeplace() <BR>{ <BR>super("添加籍贯"); <BR>Container
c=getContentPane(); <BR>c.setLayout(null); <BR>connect_db();
<BR><BR>addButton=new JButton("添加籍贯");
<BR>addButton.addActionListener(this);
<BR><BR>addButton.setBounds(220,20,100,30); <BR>c.add(addButton);
<BR><BR>updateButton=new JButton("修改籍贯");
<BR>updateButton.addActionListener(this);
<BR>updateButton.setEnabled(false);
<BR>updateButton.setBounds(220,70,100,30); <BR>c.add(updateButton);
<BR><BR>deleteButton=new JButton("删除籍贯");
<BR>deleteButton.setEnabled(false);
<BR>deleteButton.addActionListener(this);
<BR>deleteButton.setBounds(220,120,100,30); <BR>c.add(deleteButton);
<BR><BR>exitButton=new JButton("退出");
<BR>exitButton.addActionListener(this);
<BR>exitButton.setBounds(250,170,60,30); <BR>c.add(exitButton);
<BR><BR><BR><BR>root=new DefaultMutableTreeNode("籍贯"); <BR>tree=new
JTree(root); <BR><BR>tree.setEditable(true);
<BR>tree.addMouseListener(new MouseHandle());
<BR>tree.addTreeSelectionListener(this);
<BR>treeModel=(DefaultTreeModel)tree.getModel();
<BR>treeModel.addTreeModelListener(this); <BR>scroller1=new
JScrollPane(); <BR>scroller1.setViewportView(tree);
<BR>scroller1.setBounds(10,10,150,200); <BR>c.add(scroller1);
<BR><BR>try <BR>{ <BR>String query1="SELECT nativeplaceName FROM
nativeplace"; <BR>rst1=state1.executeQuery(query1);
<BR><BR><BR>while(rst1.next()) <BR>{ <BR><BR>String
rst1string=rst1.getString(1); <BR>DefaultMutableTreeNode
newnode1=new DefaultMutableTreeNode(rst1string);
<BR>root.add(newnode1); <BR><BR>treeModel.reload(); <BR>} <BR>}
<BR>catch(SQLException sqlex) <BR>{ <BR>sqlex.printStackTrace();
<BR>} <BR><BR>addWindowListener(new WindowAdapter(){ <BR>public void
windowClosing(WindowEvent e) <BR>{ <BR>//System.exit(0); <BR><BR>//
close_db(); <BR>setVisible(false); <BR>} <BR>} <BR>);
<BR><BR><BR><BR>setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
<BR>setBounds(200,200,350,250); <BR>setVisible(true);
<BR><BR><BR><BR><BR>} <BR><BR><BR>public void connect_db() <BR>{
<BR><BR>try <BR>{ <BR>Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
<BR>} <BR>catch(ClassNotFoundException e1) <BR>{
<BR>e1.printStackTrace(); <BR>} <BR><BR>try <BR>{
<BR>con1=DriverManager.getConnection("jdbc:odbc:空军国防生");
<BR>state1=con1.createStatement();
<BR>state2=con1.createStatement(); <BR><BR>}
<BR><BR>catch(SQLException e2) <BR>{ <BR>e2.printStackTrace(); <BR>}
<BR>} <BR><BR><BR><BR>public void actionPerformed(ActionEvent e)
<BR>{ <BR><BR><BR><BR>if(e.getSource()==addButton) <BR>{
<BR><BR>String s=JOptionPane.showInputDialog(addNativeplace.this,
<BR>"请输入要添加的籍贯名称","添加籍贯", <BR>JOptionPane.QUESTION_MESSAGE);
<BR>//怎样捕捉s为空? <BR>if(s!=null) <BR>{ <BR>DefaultMutableTreeNode
newNode= new DefaultMutableTreeNode(s); <BR><BR>int flag=1; <BR>try
<BR>{ <BR>String query2="SELECT nativeplaceName FROM nativeplace";
<BR>rst2=state2.executeQuery(query2); <BR>while(rst2.next()) <BR>{
<BR>String results=rst2.getString(1); <BR>if(results.equals(s))
<BR>{ <BR>flag=0; <BR>} <BR>} <BR><BR>if(flag==0) <BR>{
<BR>JOptionPane.showMessageDialog(addNativeplace.this,
<BR>"对不起,你输入的籍贯名称已经存在,请重新输入",
<BR>"籍贯输入错误",JOptionPane.ERROR_MESSAGE); <BR>} <BR><BR>if(flag==1)
<BR>{
<BR>treeModel.insertNodeInto(newNode,root,root.getChildCount());
<BR>String query="INSERT INTO nativeplace ("+ <BR>"nativeplaceName"+
<BR>") VALUES('"+s+"')"; <BR>state1.executeUpdate(query);
<BR>JOptionPane.showMessageDialog(addNativeplace.this,
<BR>"成功输入籍贯,请继续操作!","籍贯添加成功", <BR>JOptionPane.INFORMATION_MESSAGE);
<BR>} <BR><BR>} <BR>catch(SQLException sqle) <BR>{
<BR>sqle.printStackTrace(); <BR>} <BR>} <BR><BR><BR>}
<BR><BR><BR><BR>if(e.getSource()==updateButton) <BR>{ <BR>TreePath
treepath=tree.getSelectionPath(); <BR>DefaultMutableTreeNode
parentNode=(DefaultMutableTreeNode)(treepath.getLastPathComponent());
<BR>String s=JOptionPane.showInputDialog(addNativeplace.this,
<BR>"请输入要更改的籍贯名称","更改籍贯", <BR>JOptionPane.QUESTION_MESSAGE);
<BR>if(s!=null) <BR>{ <BR>DefaultMutableTreeNode newNode= new
DefaultMutableTreeNode(s);
<BR><BR>//treeModel.insertNodeInto(newNode,root,root.getChildCount());
<BR>int flag=1; <BR>try <BR>{ <BR>String query2="SELECT
nativeplaceName FROM nativeplace";
<BR>rst2=state2.executeQuery(query2); <BR>while(rst2.next()) <BR>{
<BR>String results=rst2.getString(1); <BR>if(results.equals(s))
<BR>{ <BR>flag=0; <BR>} <BR>} <BR><BR>if(flag==0) <BR>{
<BR>JOptionPane.showMessageDialog(addNativeplace.this,
<BR>"对不起,你输入的更改籍贯名称已经存在,请重新输入",
<BR>"籍贯输入错误",JOptionPane.ERROR_MESSAGE); <BR>} <BR><BR>if(flag==1)
<BR>{ <BR>String query="UPDATE nativeplace SET
nativeplaceName='"+s+"' WHERE nativeplaceName='"+parentNode+"'";
<BR>state1.executeUpdate(query); <BR>state1.executeUpdate(query);
<BR>JOptionPane.showMessageDialog(addNativeplace.this,
<BR>"成功修改籍贯名称,请继续操作!","籍贯修改成功",
<BR>JOptionPane.INFORMATION_MESSAGE); <BR>} <BR><BR>}
<BR>catch(SQLException sqle) <BR>{ <BR>sqle.printStackTrace(); <BR>}
<BR>} <BR>} <BR>if(e.getSource()==deleteButton) <BR>{ <BR>TreePath
treepath=tree.getSelectionPath(); <BR>DefaultMutableTreeNode
parentNode=(DefaultMutableTreeNode)(treepath.getLastPathComponent());
<BR>int result= JOptionPane.showConfirmDialog(addNativeplace.this,
<BR>"删除籍贯后该籍贯将不存在,是否确定要删除所选籍贯?", <BR>"
是否删除籍贯",JOptionPane.YES_NO_OPTION); <BR>TreeNode
parent1Node=(TreeNode)parentNode.getParent();
<BR><BR>if(treepath!=null) <BR>{ <BR>if(parent1Node!=null) <BR>{
<BR>if(result==JOptionPane.YES_OPTION) <BR>{ <BR>try <BR>{
<BR>String query="DELETE FROM nativeplace WHERE
nativeplaceName='"+parentNode+"'"; <BR>state1.executeUpdate(query);
<BR>} <BR>catch(SQLException sqle) <BR>{ <BR>sqle.printStackTrace();
<BR>} <BR>treeModel.removeNodeFromParent(parentNode); <BR>} <BR>}
<BR>} <BR>} <BR><BR>if(e.getSource()==exitButton) <BR>{
<BR><BR>dispose(); <BR>} <BR><BR>} <BR><BR><BR>public void
valueChanged(TreeSelectionEvent e) <BR>{ <BR>TreePath path =
e.getNewLeadSelectionPath(); <BR><BR>int
nodenumber=path.getPathCount(); <BR>if(path!=null) <BR>{
<BR>if(nodenumber==2) <BR>{ <BR>updateButton.setEnabled(true);
<BR>deleteButton.setEnabled(true); <BR>addButton.setEnabled(false);
<BR><BR>} <BR>else <BR>{ <BR>updateButton.setEnabled(false);
<BR>deleteButton.setEnabled(false); <BR>addButton.setEnabled(true);
<BR>} <BR><BR>} <BR><BR><BR><BR>} <BR><BR><BR><BR>public void
treeNodesChanged(TreeModelEvent e) <BR>{ <BR>TreePath
treePath=e.getTreePath(); <BR>DefaultMutableTreeNode
node=(DefaultMutableTreeNode)treePath.getLastPathComponent();
<BR>try <BR>{ <BR>int[] index=e.getChildIndices();
<BR>node=(DefaultMutableTreeNode)node.getChildAt(index[0]); <BR>}
<BR>catch(NullPointerException exc) <BR>{ <BR>exc.printStackTrace();
<BR>} <BR><BR><BR>} <BR>public void treeNodesInserted(TreeModelEvent
e) <BR>{ <BR><BR>} <BR>public void treeNodesRemoved(TreeModelEvent
e) <BR>{ <BR><BR>} <BR>public void
treeStructureChanged(TreeModelEvent e) <BR>{ <BR><BR>}
<BR><BR><BR>class MouseHandle extends MouseAdapter <BR>{ <BR>public
void mousePressed(MouseEvent e) <BR>{ <BR><BR>JTree
tree=(JTree)e.getSource(); <BR>int
rowLocation=tree.getRowForLocation(e.getX(),e.getY()); <BR>TreePath
treePath=tree.getSelectionPath(); <BR><BR>if(rowLocation!= -1) <BR>{
<BR>TreeNode treenode=(TreeNode)treePath.getLastPathComponent();
<BR><BR>} <BR>} <BR>} <BR><BR><BR>}</SPAN></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=spaceRow colSpan=2 height=1><IMG height=1 alt=""
src="www_jieesoft_com 主题回顾 - JTree和数据库的一点小bug,请指教,谢谢.files/spacer.gif"
width=1></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -