java对数据库数据用表格形式输出,为什么界面里面不显示表格,请具体之处那句代码错误
mportjava.awt.BorderLayout;importjava.sql.Connection;importjava.sql.DriverManager;imp...
mport java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
public class Search extends JFrame{
private Connection con;
Search(){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(200, 200, 600, 400);
setLayout(new BorderLayout());
Vector<String> vector = new Vector<String>();
vector.add("vfirstname".toUpperCase());
vector.add("vlastname".toUpperCase());
vector.add("csex".toUpperCase());
vector.add("dtestdate".toUpperCase());
vector.add("ntestscore".toUpperCase());
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.9.45:1521:recruit","user1","user1");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Statement stat = con.createStatement();
String s="select vfirstname, vlastname,csex,dtestdate,ntestscore from externalcandidate";
Vector data=new Vector();
ResultSet rs = stat.executeQuery(s);
while (rs.next()) {
String s1 = rs.getString(1);
String s2 = rs.getString(2);
String s3 = rs.getString(3);
String s4 = rs.getString(4);
String s5 = rs.getString(5);
Vector<String> row = new Vector<String>();
row.add(s1);
row.add(s2);
row.add(s3);
row.add(s4);
row.add(s5);
data.add(row);
}
DefaultTableModel tableModel = new DefaultTableModel(data,vector);
JTable table = new JTable(tableModel);
table.setShowHorizontalLines(true);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
scrollPane.add(table);
add(scrollPane);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Search().setVisible(true);
}
} 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
public class Search extends JFrame{
private Connection con;
Search(){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(200, 200, 600, 400);
setLayout(new BorderLayout());
Vector<String> vector = new Vector<String>();
vector.add("vfirstname".toUpperCase());
vector.add("vlastname".toUpperCase());
vector.add("csex".toUpperCase());
vector.add("dtestdate".toUpperCase());
vector.add("ntestscore".toUpperCase());
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.9.45:1521:recruit","user1","user1");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Statement stat = con.createStatement();
String s="select vfirstname, vlastname,csex,dtestdate,ntestscore from externalcandidate";
Vector data=new Vector();
ResultSet rs = stat.executeQuery(s);
while (rs.next()) {
String s1 = rs.getString(1);
String s2 = rs.getString(2);
String s3 = rs.getString(3);
String s4 = rs.getString(4);
String s5 = rs.getString(5);
Vector<String> row = new Vector<String>();
row.add(s1);
row.add(s2);
row.add(s3);
row.add(s4);
row.add(s5);
data.add(row);
}
DefaultTableModel tableModel = new DefaultTableModel(data,vector);
JTable table = new JTable(tableModel);
table.setShowHorizontalLines(true);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
scrollPane.add(table);
add(scrollPane);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new Search().setVisible(true);
}
} 展开
展开全部
我调了一下,觉得是那个 JScrollPane初始化问题,64~66行左右。改动下面三行代码
JScrollPane scrollPane = new JScrollPane(table); //直接用table初始化JScrollPane
scrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
// scrollPane.add(table); 此句注释或删掉。
我在做一个玩意,你的倒是启发我了
JScrollPane scrollPane = new JScrollPane(table); //直接用table初始化JScrollPane
scrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
// scrollPane.add(table); 此句注释或删掉。
我在做一个玩意,你的倒是启发我了
展开全部
JScrollPane中的JTable并不是后面利用add添加的,而是在JScrollPane初期声明的时候利用构造函数添加的new JScrollPane(JTable jt),这里的jt就是你要显示的表格。
你的程序中做以下修改:
⑴54行的“JScrollPane scrollPane = new JScrollPane();”改为“JScrollPane scrollPane = new JScrollPane(table);”
⑵56行的“scrollPane.add(table);”去除
你的程序中做以下修改:
⑴54行的“JScrollPane scrollPane = new JScrollPane();”改为“JScrollPane scrollPane = new JScrollPane(table);”
⑵56行的“scrollPane.add(table);”去除
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有报错?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |