Topic: 请教一个问题,谢谢

  Print this page

1.请教一个问题,谢谢 Copy to clipboard
Posted by: Bluebluesea
Posted on: 2007-04-02 09:02

是关于数据库连接问题的,请大家帮帮忙,我实在弄不出来了,JAVA源程序是这样的:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.sql.*;
import javax.swing.table.*;
import javax.swing.tree.*;
public class classBrowse extends JFrame implements TreeSelectionListener{
Statement stmt;
ResultSet rs,rs1;
DefaultTableModel dtm;
String[] title;
JTree tree;
JTable table;
DefaultTreeModel treeModel=null;
JScrollPane scroll1,scroll2;
JSplitPane splitPane;
public classBrowse(){
   try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    
    }
    catch(Exception e){e.printStackTrace();}
   try{  
   Connection con=DriverManager.getConnection("jdbc:odbc:xsgl");
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
   DefaultMutableTreeNode root=new DefaultMutableTreeNode("专业");
     treeModel=new DefaultTreeModel(root);
     initTree(root,0);
     tree=new JTree(treeModel);
     scroll1=new JScrollPane(tree);
     tree.addTreeSelectionListener(this);
     rs=stmt.executeQuery("select classclass.name as 班级,xsda.no as 学号,xsda.name as 姓名 from xsda,classclass where xsda.classID=classclass.id");
     ResultSetMetaData dbmd=rs.getMetaData();
     title=new String[dbmd.getColumnCount()];
     for(int i=1;i<=dbmd.getColumnCount();i++)
       title[i-1]=dbmd.getColumnNameLight Bulb;
     dtm=new DefaultTableModel(null,title);
     table=new JTable(dtm);
     scroll2=new JScrollPane(table);
     initTable();
     splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scroll1,scroll2);
     splitPane.setOneTouchExpandable(true);
     splitPane.setDividerLocation(200);
     this.getContentPane().add(splitPane,null);
   }catch(SQLException e){
     System.out.printlnEnvelope;
   }
   setTitle("分类查询学生信息");
   setSize(500,400);
   setVisible(true);
}
void initTree(DefaultMutableTreeNode root,int id){
   DefaultMutableTreeNode node=null;
   try{
     Connection con=DriverManager.getConnection("jdbc:odbc:xsgl");
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
     ResultSet rs=stmt.executeQuery("select * from classclass where parent="+id);
     while(rs.next()){
       int nno=rs.getInt("id");
       String nname=rs.getString("name").trim();
       node=new DefaultMutableTreeNode(nname);
       treeModel.insertNodeInto(node,root,root.getChildCount());
       initTree(node,nno);
     }
   }catch(Exception e){}
   finally{
     try{
       rs.close();
     }catch(Exception e){}
   }
}
public void valueChanged(TreeSelectionEvent e){
  JTree tree=(JTree)e.getSource();
  DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
  String name=((String)selectionNode.getUserObject());
  String sql="select classclass.name as 班级,xsda.no as 学号,xsda.name as 姓名 from xsda,classclass where xsda.classID=classclass.id";
  if(!name.equals("专业"))
    sql=sql+"and classID in (select id from classclass where rtrim(ltrim(str(id)))like (select ltrim(rtrim(str(id)))+'%'from classclass where rtrim(ltrim(name))='"+name+"')";
  try{
    rs=stmt.executeQuery(sql);
    initTable();
  }catch(Exception ee){}
}
void initTable(){
  dtm.setRowCount(0);
  try{
    rs.beforeFirst();
    while(rs.next()){
      Vector v1=new Vector();
      for(int i=1;i<=title.length;i++)
        v1.addElement(rs.getStringLight Bulb);
      dtm.addRow(v1);
    }
  }catch(SQLException e){e.printStackTrace();}
  dtm.fireTableStructureChanged();
}
  public static void main(String[] args) {
        new classBrowse();
  }

}
配置了数据源和数据库的名称一样xsgl
此数据库里面还有两张表:表xsda有字段no,classID,name,sex,birthDate,speciality,address,isMember,resume,picture
表classclass有字段:id,parent,name
请大家帮忙调试一下,非常感谢

2.Re:请教一个问题,谢谢 [Re: Bluebluesea] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2007-04-06 12:47

package mypackage;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.List;
import javax.swing.tree.*;
import javax.swing.table.*;

public class ClassBrowse
{
  public static void main(String[] args) throws Exception
  {
    new MyFrame();
  }
  
}

class MyFrame extends JFrame
{
  public MyFrame()
  {
    setSize(500, 500);
    setResizable(false);
    setTitle("ShowStudentInfomation");
    setLayout(null);
    JScrollPane treePanel = TreePanel.getScrollPanel();
    JScrollPane showPanel = ShowStudentPanel.getScrollPanel();
    
    treePanel.setBounds(0, 0, 100, 500);
    showPanel.setBounds(100, 0, 400, 500);
    Container contentPane = getContentPane();
    contentPane.add(treePanel);
    contentPane.add(showPanel);
    this.setVisible(true);
  }
  
  
}

class TreePanel
{
  public static JScrollPane getScrollPanel()
  {
    try
    {
      ConnectDB m = ConnectDB.getInstance();
      DefaultMutableTreeNode root = m.getRootOrg();

      m.selectSonNode(root);
      
      JTree tree = new JTree(root);
      panel = new JScrollPane(tree);
    }
    catch(Exception e)
    {
      panel = new JScrollPane();
      System.out.printlnEnvelope;
    }
    
    return panel;
  }
  
  private static JScrollPane panel;
}

class ShowStudentPanel extends JScrollPane
{
  public static JScrollPane getScrollPanel()
  {
    getTable("111");
    return new JScrollPane(table);
  }
  
  public static JTable getTable(String classId)
  {
    //classId = "111";
    try
    {
      String[][] array = ConnectDB.getInstance().selectStudentByClassId(classId);
      String[] names = {"学号", "姓名", "性别", "出生日期", "电子邮件"};
      
      for(int i = 0; i < array.length; i++)
      {
        for(int j = 0; j < array[i].length; j++)
        {
          System.out.println(array[i][j]);
        }
      }
      
      table = new JTable(array, names);

    }
    catch(Exception e)
    {
      System.out.printlnEnvelope;
    }
    
    return table;
  }
  
  private static JTable table;
}

class ConnectDB
{
  public static ConnectDB getInstance()
  {
    return new ConnectDB();
  }
  
  private ConnectDB()
  {
    list = new ArrayList();
  }
  
  public Connection getConnection()
  {
    try
    {
      Class.forName(driverClass);
      con=DriverManager.getConnection(URL);
    }
    catch(Exception e)
    {
      System.out.printlnEnvelope;
    }
    
    return con;
  }
  
  public void close()
  {
    try
    {
      if(set != null)
      {
        set.close();
      }
      
      if(st != null)
      {
        st.close();
      }
      
      if(con != null)
      {
        con.close();
      }
    }
    catch(Exception e)
    {
      System.out.printlnEnvelope;
    }
  }
  
  public void selectStudentByStudentId(String studentId) throws Exception
  {
    getConnection();
    st = con.prepareStatement(sql1);
    st.setString(1, studentId);
    set = st.executeQuery();
    while(set.next())
    {
      System.out.println(set.getString(1));
    }
    
    close();
  }
  
  public String[][] selectStudentByClassId(String classId) throws Exception
  {
    getConnection();
    st = con.prepareStatement(sql2, ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    st.setString(1, classId);
    set = st.executeQuery();
    set.last();
    
    int maxRowNum = set.getRow();
    String[][] array = new String[maxRowNum][5];
    int row = 0;
    int i = 0;
    
    set.beforeFirst();
    while(set.next())
    {
      for(i = 1; i <= 5; i++)
      {
        array[row][i - 1] = set.getStringLight Bulb;
        System.out.println(array[row][i - 1]);
      }
      
      row++;
    }
    
    close();
    
    return array;
  }
  
  public void selectSonNode(DefaultMutableTreeNode root) throws Exception
  {
    getConnection();
    
    selectSonOrg(root, "100");
    
    try
    {
      if(st != null)
      {
        st.close();
      }
      
      if(con != null)
      {
        con.close();
      }
    }
    catch(Exception e)
    {
      System.out.printlnEnvelope;
    }
  }
  
   private void selectSonOrg(DefaultMutableTreeNode root, String id) throws Exception
  {
    String orgName = "";
    
  
    st = con.prepareStatement(sql4);
    st.setString(1, id);
    ResultSet set = st.executeQuery();
    
    while(set.next())
    {
      id = set.getString(1);
      orgName = set.getString(2);

      DefaultMutableTreeNode node = new DefaultMutableTreeNode(orgName);
      root.add(node);
      
      System.out.println(node);

      selectSonOrg(node, id);
    }
    
    set.close();
  }
  
  public DefaultMutableTreeNode getRootOrg() throws Exception
  {
    String id = "";
    String orgName = "";
    DefaultMutableTreeNode root = null;
    
    getConnection();
    st = con.prepareStatement(sql3);
    set = st.executeQuery();
    while(set.next())
    {
      id = set.getString(1);
      orgName = set.getString(2);

      root = new DefaultMutableTreeNode(orgName);
    }
    
    close();
    
    return root;
  }
  
  private Connection con;
  private PreparedStatement st;
  private ResultSet set;
  private List list;
  
  public final String driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
  public final String URL = "jdbc:odbc:mydb";
  public final String userName = "";
  public final String password = "";
  
  public final String sql1 = "select id, name, sex, birthday, email from student where id = ?";
  public final String sql2 = "select id, name, sex, birthday, email from student where classid = ?";
  public final String sql3 = "select id, orgName, parent from org where parent is null";
  public final String sql4 = "select id, orgName, parent from org where parent = ?";
}

写了一部分,点击树的叶子的事件还没有添加,希望对你有所启发

3.Re:请教一个问题,谢谢 [Re: Bluebluesea] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2007-04-06 12:52



4.Re:请教一个问题,谢谢 [Re: Bluebluesea] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2007-04-06 12:54



5.Re:请教一个问题,谢谢 [Re: Bluebluesea] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2007-04-06 13:00

你的这个帖子应该发到Java基础中


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923