我目前正在NetBeans IDE中开发一个程序。我创建了一个很好的GUI,我创建了MS Access数据库。我在JTable
中显示MS Access数据时遇到问题。我想避免使用向量,如我在互联网上找到的大多数教程所示,因为我还在高中,这些知识超出了我的范围。
任何指向正确方向的人都会非常感激!
这是我的代码:
String[] columnNames = {"First Name",
"Last Name",
"Category",
"Amount"
};
Object[] row =new Object[4];
JLabel lbl=new JLabel("Add New Property");
lbl.setBounds(100,200,200,100);
lbl.setVisible(true);
invntryfrm.add(lbl);
//invntryfrm.setVisible(true);
JPanel panel=new JPanel();
panel.setBounds(20,200,680,100);
panel.setBackground(Color.WHITE);
invntrybck.add(panel);
DefaultTableModel model=new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
JTable tabel=new JTable();
tabel.setBounds(100,20,700,400);
tabel.setBackground(Color.DARK_GRAY);
tabel.setForeground(Color.WHITE);
tabel.setModel(model);
tabel.setPreferredScrollableViewportSize(new Dimension(500,50));
tabel.setFillsViewportHeight(true);
JScrollPane pane=new JScrollPane(tabel);
panel.add(pane);
try{
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\MUHAMMAD SHAHAB\\real estate.accdb");
String sql="select Username,Password,Country,City from simba";
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
答案 0 :(得分:6)
(1)。首先在库文件夹中添加rs2Xml.jar,然后在代码中进行以下更改:
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\MUHAMMAD SHAHAB\\real estate.accdb");
String sql="select Username,Password,Country,City from simba";
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
tabel.setModel(DbUtils.resultSetToTableModel(rs));
我希望这对你有用。
答案 1 :(得分:2)
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import static java.lang.System.out;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JavaApplication16 {
public static void main(String[] args) {
String driver="org.apache.derby.jdbc.ClientDriver";
String url="jdbc:derby://localhost:1527/simbadb";
String username="simbadb", password="simbadb";
Connection con=null;
try
{
Class.forName(driver);
con=DriverManager.getConnection(url, username, password);
String query="select * from simba";
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
int rowcount=0;
ResultSet rs=stmt.executeQuery(query);
ResultSetMetaData rsmd=rs.getMetaData();
int columncount=rsmd.getColumnCount();
while(rs.next())
{
rowcount++;
}
out.println("number of records: "+rowcount);
String[][] rowdata=new String[rowcount][columncount];
rs.beforeFirst();
int rowindex=0;
while(rs.next())
{
int columnindex=0;
for(int i=1;i<=columncount;i++)
{
rowdata[rowindex][columnindex]=rs.getString(i);
columnindex++;
}
rowindex++;
}
String[] coldata=new String[columncount];
int colindex=0;
for(int i=1;i<=columncount;i++)
{
coldata[colindex]=rsmd.getColumnName(i);
colindex++;
}
rs.close();
DefaultTableModel dtm=new DefaultTableModel(rowdata, coldata);
JFrame frame=new JFrame();
JTable table=new JTable();
table.setModel(dtm);
Container c=frame.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEFT));
c.add(new JScrollPane(table));
frame.setSize(new Dimension(500, 500));
frame.setVisible(true);
}
catch(Exception e)
{
out.println(e);
}
finally
{
try
{
if(con!=null)
{
con.close();
out.println("closed");
}
}
catch(Exception e)
{
out.println(e);
}
}
}
}
<小时/> 在jdk 8中,不支持和删除JdbcOdbcDriver!所以我必须测试它 德比数据库。
答案 2 :(得分:0)
只需添加
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
在尝试捕获块中