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,跟着教程码的,教材没报错,很久了没解决,谢谢,
1 回答

陪伴而非守候
TA贡献1757条经验 获得超8个赞
public Test2(Object rs, Object ps, Object ct)
替换为
public Test2(ResultSet rs, PreparedStatement ps, Connection ct)
添加回答
举报
0/150
提交
取消