我想在JAVA中显示多行SQL 查询记录,该如何实现?谁能给我介绍一些JDBC的资料,小弟现在这鞠躬了!
我是个JAVA初学者,很多不懂,遇到问题没有良师益友的指点与讨论,一时半刻无法解决,欲哭无泪,谁愿意与我经常交流?(最好是用邮箱联系,我们公司不允许上QQ);QQ是274375180(加我时注明:JAVA)!愿与同道中人携手共进!共讨JAVA!!!

解决方案 »

  1.   

    我想在JAVA中显示多行SQL 查询记录,该如何实现?
    ----------------------------------------------
    太概括了吧???
      

  2.   

    比如说我有个表 Memners,字段有mno,mname,sex,age,class;现有语句stmt.executeQuery("select mno,mname,sex,age,class from Members");要想将我查询的结果全部显示在一个表格中,该如何实现呢?
      

  3.   

    我用ResultSet只会显示单个记录,不会显示一行或多行记录,请高手指教啊!!!顶啊!!
      

  4.   

    最简单的,循环出来就行了
    while (rs.next)
    {
       
    }
      

  5.   

    while (rs.next)
    {
       System.out.println(rs.getInt("mno")+rs.getString("mname")+rs.getString("sex")..
    }
      

  6.   

    int[]   sRow   =   table.getSelectedRows();   
      

  7.   

    /**
     * 一个JTree的例子,可以实现点击节点事件,增加、删除节点
     *//**
     * @author [email protected]
     *
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.tree.*;
    public class JTreeTest2 { /**
     * 定义控件
     */
    JFrame frm=null;
    JTree tree=null;
    JScrollPane jspTree=null;
    JPanel btnP=null,txtP=null;//btnP用于容纳按钮addbtn和delbtn,txtP用于容纳文本框txta
    JTextArea txta=null;
    JTextField childrentxt=null;
    JButton delbtn=null,addbtn=null;
    JLabel meslb=null;

    public JTreeTest2() {
    // TODO Auto-generated constructor stub
    //构造函数
    frm=new JFrame("JTree Test!");
    Container cp=frm.getContentPane();
    BorderLayout layout=new BorderLayout();
    cp.setLayout(layout);
    //创建树
    DefaultMutableTreeNode root=new DefaultMutableTreeNode("选择...");
    DefaultMutableTreeNode color=new DefaultMutableTreeNode("颜色");
    DefaultMutableTreeNode white=new DefaultMutableTreeNode("白色");
    DefaultMutableTreeNode black=new DefaultMutableTreeNode("黑色");
    DefaultMutableTreeNode blue =new DefaultMutableTreeNode("蓝色");
    DefaultMutableTreeNode paper=new DefaultMutableTreeNode("纸张");
    DefaultMutableTreeNode A3=new DefaultMutableTreeNode("A3纸");
    DefaultMutableTreeNode A4=new DefaultMutableTreeNode("A4纸");
    DefaultMutableTreeNode A5=new DefaultMutableTreeNode("A5纸");
    root.add(color);
    color.add(white);
    color.add(black);
    color.add(blue);
    paper.add(A3);
    paper.add(A4);
    paper.add(A5);
    root.add(paper);
    tree=new JTree(root);
    tree.addMouseListener(new TreeMouseLis());
    jspTree=new JScrollPane(tree);

    txta=new JTextArea();
    txta.setSize(300,100);

    delbtn=new JButton("删除");
    delbtn.setSize(60,30);
    addbtn=new JButton("添加");
    addbtn.setSize(60,30);
    btnP=new JPanel();
    btnP.setLayout(layout);
    btnP.add(addbtn,BorderLayout.CENTER);
    btnP.add(delbtn,BorderLayout.CENTER);
    addbtn.addActionListener(new ActionLis());
    delbtn.addActionListener(new ActionLis());

    meslb=new JLabel("增加节点");
    childrentxt=new JTextField();
    childrentxt.setSize(80,30);
    txtP=new JPanel();
    txtP.add(meslb,BorderLayout.EAST);
    txtP.add(childrentxt,BorderLayout.CENTER);

    cp.add(tree,layout.EAST);
    cp.add(txta,layout.NORTH);
    cp.add(txtP,layout.CENTER);
    cp.add(btnP,layout.SOUTH); frm.setVisible(true);
    frm.setSize(500,400);
    frm.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    }
    //定义按钮处理事件
    class ActionLis implements ActionListener
    {
    public void actionPerformed(ActionEvent ex)
    {
    if((JButton)ex.getSource()==addbtn)//添加按钮事件
    {
    txta.setText("添加一个节点");
    }
    else if((JButton)ex.getSource()==delbtn)//删除按钮事件
    {
    txta.setText("删除一个节点");
    }
    }
    }
    //定义鼠“标按”下事件
    class TreeMouseLis extends MouseAdapter//选择节点事件
    {
    public void mousePressed(MouseEvent mex)
    {
    JTree tree=(JTree)mex.getSource();
    int clickRow=tree.getRowForLocation(mex.getX(),mex.getY());
    if(clickRow!=-1)
    {
    TreePath treePath=tree.getPathForRow(clickRow);
    TreeNode treeNode=(TreeNode)treePath.getLastPathComponent();//什么意思?
    txta.setText("你选择的是:"+treeNode);
    }
    }
    } /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new JTreeTest2(); }}
    运行以上代码,奇怪的是只显示一个窗口,其他控件不显示,谁能帮我解决啊????
      

  8.   

    用循环就行了,while,for ,iterator都行
      

  9.   

    import java.sql.*;
    public class DbTest
    {   private Connection conn=null;
        private ResultSet rs=null;
        private Statement stm=null;
    public DbTest(){}
    public void openOdbcDb(String dsnn,String user,String password)
    {try{
    String url="jdbc:odbc:"+dsnn;
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             conn=DriverManager.getConnection(url,user,password);
        
    }catch(Exception e){System.err.println("aq.executeQuery: " + e.getMessage());}
            }
        
    public void openJdbcDb(String dsnn)
    {openJdbcDb(dsnn,"sa","sa");
            }
    public void openJdbcDb(String dsnn,String user,String password)
    {try{Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dsnn; 
            conn= DriverManager.getConnection(url,user,password); 
    }catch(Exception e){System.err.println("aq.executeQuery: " + e.getMessage());}
            }
    public ResultSet executeQuery(String sql)
    {rs=null;
         try{
     stm=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
         rs=stm.executeQuery(sql);

    }catch(Exception e){System.err.println("aq.executeQuery: " + e.getMessage());}
    return rs;
    }
        
    public void executeUpdate(String sql)
    { try{
      stm=conn.createStatement();
          stm.executeUpdate(sql);
     }catch(Exception e){System.err.println("aq.executeQuery: " + e.getMessage());}
         }
        public void closeDb()
    {if(rs!=null)
      {try{rs.close();
           rs=null;
            }catch(Exception e){}
           }
       if(stm!=null)
       {try{stm.close();
       stm=null;
       }catch(Exception e){}
       }
       if(conn!=null)
       {try{
       conn.close();
       conn=null;
       }
       catch(Exception e){}
       }
        }
    public static void main(String [] args)throws Exception
    {DbTest test=new DbTest();
         test.testDb("select * from liuyan_tb join fuzhuang_tb on liuyan_tb.ID=fuzhuang_tb.ID");

         }
    public  void testDb(String sql)throws Exception
    { ResultSet rst=null;
      openJdbcDb("clother");
      System.out.println(sql);
          rst=executeQuery(sql);
    for(int j=1; j<=rst.getMetaData().getColumnCount(); j++)
     {System.out.print(rst.getMetaData().getColumnName(j)+"   ");}
       System.out.println("");
         while (rst.next())
         {  for(int j=1; j<=rst.getMetaData().getColumnCount(); j++)
         {
           System.out.print( rst.getObject(j)+"   ");
     }
     System.out.println("");
         }

        closeDb();
    }
    }
      

  10.   

    楼主要的不是数据库的问题,而是如何实现JTable.
    JDK里有源码的。
      

  11.   

    注意上边说的 应该是
    while(rs.next()){
    System.out.println(rs.getString(1));
    }
    而不是rs.next ,应该是是有括号的
      

  12.   

    书店上的jdbc书很多可以去看看,这个东西是很简单的没有多少要学的,或是你去网上下载API文档吧