猿问

The method next() is undefined for the type Object ?

package com.test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Test2 extends JFrame{

    Vector rowData,columnNames;
    JTable jt = null;    
    JScrollPane jsp = null;
    
    PreparedStatement ps = null;
    Connection ct=null;
    ResultSet rs=null;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
    public Test2(Object rs, Object ps, Object ct){
        columnNames = new Vector();
        
        columnNames.add("学号");
        columnNames.add("名字");
        columnNames.add("性别");
        columnNames.add("年龄");
        columnNames.add("籍贯");
        columnNames.add("系别");
        
        rowData = new Vector();
         try {
            Class.forName("com.mysql.jdbc.Driver");
            ct = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3066/"
                        + "book?CharacterEncoding=gbk","root","root");
            ps = ((Connection) ct).prepareStatement("select * from stu");
            rs = ((PreparedStatement) ps).executeQuery();
            
        //标记A,next下划线报错,The method getString(int) is undefined for the type Object            
        //标记B,getString下划线报错,连续4行,The method getString(int) is undefined for the type Object    
        while(rs.next()){
                Vector hang = new Vector();
                hang.add( rs.getString(1));
                hang.add(rs.getString(2));
                hang.add(rs.getString(3));
                hang.add(rs.getInt(4));
                hang.add(rs.getString(5));
                hang.add( rs.getString(6));
                
                rowData.add(hang);
            }
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
//标记C, .close报错,The method close() is undefined for the type Object
        
}finally{
            try{
                if(rs!=null ) rs.close();
                if(ps!=null)  ps.close();
                if(ct!=null)   ct.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        
        jt = new JTable(rowData,columnNames);
        jsp = new JScrollPane(jt);
        
        this.add(jsp);
        this.setSize(400, 350);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        
    }

}

无法上传图片,
标记A
标记B
标记C,
跟着教程码的,教材没报错,很久了没解决,
谢谢,

至尊宝的传说
浏览 5400回答 1
1回答

陪伴而非守候

public Test2(Object rs, Object ps, Object ct)替换为public Test2(ResultSet rs, PreparedStatement ps, Connection ct)
随时随地看视频慕课网APP

相关分类

Java
我要回答