将数据从数据库插入到表中
这是我的代码,它不起作用,因为显示空表,没有任何行和列 有人可以告诉问题出在哪里吗
public class test extends JFrame {
private final JButton pridetiNaujaButton = new JButton();
private final JButton pasalintiButton = new JButton();
private final JButton redagotiButton = new JButton();
private final JButton paieskaButton = new JButton();
final GtFromDb baz=new GtFromDb();
private Vector <Vector<String>> data;
private Vector<String> header;
private final JScrollPane scrollPane = new JScrollPane();
private final JTable table = new JTable(data,header);
public static void main(String args[]) {
try {
test frame = new test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public test() {
super();
setBounds(100, 100, 781, 412);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
jbInit();
} catch (Throwable e) {
e.printStackTrace();
}
GtFromDb db=new GtFromDb();
try {
data=db.getClient();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
header=new Vector<String>();
header.add("Imones pavadinimas");
header.add("vardas");
header.add("pavarde");
header.add("salis");
header.add("Adresas");
header.add("telefonas");
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
getContentPane().add(pridetiNaujaButton);
pridetiNaujaButton.setText("Prideti nauja");
pridetiNaujaButton.setBounds(0, 0, 106, 26);
getContentPane().add(pasalintiButton);
pasalintiButton.setText("Pasalinti");
pasalintiButton.setBounds(112, 0, 106, 26);
getContentPane().add(redagotiButton);
redagotiButton.setText("Redagoti");
redagotiButton.setBounds(224, 0, 106, 26);
getContentPane().add(paieskaButton);
paieskaButton.setText("Paieska");
paieskaButton.setBounds(336, 0, 106, 26);
getContentPane().add(scrollPane);
scrollPane.setBounds(10, 48, 745, 316);
scrollPane.setViewportView(table);
}
public class GtFromDb
{ Connection connect;
Statement zadanie;
String sql;
ResultSet dane;
// Map<String,String> carmap =new HashMap<String,String>();
// Map<String,String> ownmap =new HashMap<String,String>();
void DbConnection() throws SQLException
{
String baza="jdbc:odbc:dielektric_repair";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("Connection error");}
connect = DriverManager.getConnection(baza,"","");
}
public Vector getClient() throws Exception
{
Vector <Vector<String>> clientVector=new Vector<Vector<String>>();
try
{DbConnection();
PreparedStatement zadanie = connect.prepareStatement("SELECT * FROM Clients");
dane = zadanie.executeQuery();
while(dane.next()) {
Vector <String> client=new Vector<String>();
String imonesPav=dane.getString("Imones_pavadinimas");
String vardas = dane.getString("Vardas");
String pavarde = dane.getString("Pavarde");
String salis = dane.getString("Salis");
String adresas=dane.getString("Adresas");
String telefonas = dane.getString("Telefonas");
if (imonesPav != null) {imonesPav = imonesPav.trim();
client.add(imonesPav);}
if (vardas != null) {vardas = vardas.trim();
client.add(vardas);}
if (pavarde != null) {pavarde = pavarde.trim();
client.add(pavarde);}
if (salis != null) {salis =salis.trim();
client.add(salis);}
if (adresas != null) {adresas =adresas.trim();
client.add(adresas);}
if (telefonas != null) {telefonas = telefonas.trim();
client.add(telefonas);}
}zadanie.close();
}catch(SQLException e){}
return clientVector;
}
}
public JScrollPane getScrollPane() {
return scrollPane;
}
}
there's my code, it doesn't work because shows emty table without any rows ir columns
can someone tel where is problem
public class test extends JFrame {
private final JButton pridetiNaujaButton = new JButton();
private final JButton pasalintiButton = new JButton();
private final JButton redagotiButton = new JButton();
private final JButton paieskaButton = new JButton();
final GtFromDb baz=new GtFromDb();
private Vector <Vector<String>> data;
private Vector<String> header;
private final JScrollPane scrollPane = new JScrollPane();
private final JTable table = new JTable(data,header);
public static void main(String args[]) {
try {
test frame = new test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public test() {
super();
setBounds(100, 100, 781, 412);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
jbInit();
} catch (Throwable e) {
e.printStackTrace();
}
GtFromDb db=new GtFromDb();
try {
data=db.getClient();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
header=new Vector<String>();
header.add("Imones pavadinimas");
header.add("vardas");
header.add("pavarde");
header.add("salis");
header.add("Adresas");
header.add("telefonas");
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
getContentPane().add(pridetiNaujaButton);
pridetiNaujaButton.setText("Prideti nauja");
pridetiNaujaButton.setBounds(0, 0, 106, 26);
getContentPane().add(pasalintiButton);
pasalintiButton.setText("Pasalinti");
pasalintiButton.setBounds(112, 0, 106, 26);
getContentPane().add(redagotiButton);
redagotiButton.setText("Redagoti");
redagotiButton.setBounds(224, 0, 106, 26);
getContentPane().add(paieskaButton);
paieskaButton.setText("Paieska");
paieskaButton.setBounds(336, 0, 106, 26);
getContentPane().add(scrollPane);
scrollPane.setBounds(10, 48, 745, 316);
scrollPane.setViewportView(table);
}
public class GtFromDb
{ Connection connect;
Statement zadanie;
String sql;
ResultSet dane;
// Map<String,String> carmap =new HashMap<String,String>();
// Map<String,String> ownmap =new HashMap<String,String>();
void DbConnection() throws SQLException
{
String baza="jdbc:odbc:dielektric_repair";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){System.out.println("Connection error");}
connect = DriverManager.getConnection(baza,"","");
}
public Vector getClient() throws Exception
{
Vector <Vector<String>> clientVector=new Vector<Vector<String>>();
try
{DbConnection();
PreparedStatement zadanie = connect.prepareStatement("SELECT * FROM Clients");
dane = zadanie.executeQuery();
while(dane.next()) {
Vector <String> client=new Vector<String>();
String imonesPav=dane.getString("Imones_pavadinimas");
String vardas = dane.getString("Vardas");
String pavarde = dane.getString("Pavarde");
String salis = dane.getString("Salis");
String adresas=dane.getString("Adresas");
String telefonas = dane.getString("Telefonas");
if (imonesPav != null) {imonesPav = imonesPav.trim();
client.add(imonesPav);}
if (vardas != null) {vardas = vardas.trim();
client.add(vardas);}
if (pavarde != null) {pavarde = pavarde.trim();
client.add(pavarde);}
if (salis != null) {salis =salis.trim();
client.add(salis);}
if (adresas != null) {adresas =adresas.trim();
client.add(adresas);}
if (telefonas != null) {telefonas = telefonas.trim();
client.add(telefonas);}
}zadanie.close();
}catch(SQLException e){}
return clientVector;
}
}
public JScrollPane getScrollPane() {
return scrollPane;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几个问题:
1) 您没有将
client
对象添加到clientVector
中。使用clientVector.add(client)
在 ResultSet 的每次迭代后添加客户端。2) 您已使用 null
data
和header
变量初始化表。首先,初始化data
和header
变量,然后初始化table
,如下所示:3) 您捕获了一个SQLException,但没有对其执行任何操作。因此,如果运行查询时出现错误,您将不会知道。尝试打印堆栈跟踪:
There are a few issues:
1) You are not adding the
client
objects to theclientVector
. UseclientVector.add(client)
to add the client after each iteration of the ResultSet.2) You have initialised the table with null
data
andheader
variables. First, initialise thedata
andheader
variables and then thetable
, like this:3) You are catching a SQLException and doing nothing with it. So if there is an error running the query, you will not know about it. Try printing out the stack trace: