hi all i m facing problem populating jtable with the data of database !m using arraylist to populate jtable .but the problem is that m unable to add databse data into jtables.i m only getting the last row elements of databse.
i should be getting this data in the jtables
BookName AuthorName
sohaib asas
maroof sasa
hh ddd
hh maoof
yy yy
yy yy
x x
x x
y y
but im getting this data instead....
BookName AuthorName
y y
y y
y y
y y
y y
y y
y y
i think the problem is in Book class ! there should be some loop to ensure that each value is entered in the jtable.
m using MVC architecture so im posting just related code not the whole code .
please help me out m stuck it it from days.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewJFrame.java
*
* Created on Sep 8, 2011, 2:17:57 PM
*/
package com.company.librarySystem.bm.ui;
import com.company.librarySystem.bm.controller.AddController;
import com.company.librarySystem.bm.controller.DeleteController;
import com.company.librarySystem.bm.controller.UpdateController;
import com.company.librarySystem.bm.dto.Book;
/**
*
* @author Administrator
*/
public class NewJFrame extends javax.swing.JFrame {
javax.swing.table.DefaultTableModel defaultTableModel = new javax.swing.table.DefaultTableModel();
public NewJFrame() {
initComponents();
jTable1.setModel(defaultTableModel);
defaultTableModel.addColumn((Object) "Book Name");
defaultTableModel.addColumn((Object) "Author Name");
AddController addController = new AddController();
addController.selectAll();
jTable1.setModel(defaultTableModel);
for (Book book : addController.selectAll()) {
//here is the problem it should print aa the database data but its print the last row data
defaultTableModel.addRow(new String[]{book.BookName, book.AuthorName});
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bokName = new javax.swing.JTextField();
authorName = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
bookName = new javax.swing.JLabel();
autherName = new javax.swing.JLabel();
Add = new javax.swing.JButton();
Update = new javax.swing.JButton();
Delete = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel1.setText("Book Management");
bookName.setText("Book Name");
autherName.setText("Author Name");
Add.setText("Add");
Add.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
AddMouseClicked(evt);
}
});
Update.setText("Update");
Update.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
UpdateMouseClicked(evt);
}
});
Delete.setText("Delete");
Delete.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
DeleteMouseClicked(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 9, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 120, Short.MAX_VALUE)
);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Book name", "Author name"
}
) {
boolean[] canEdit = new boolean [] {
false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(bookName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(autherName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(authorName, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
.addComponent(bokName, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(Add, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addComponent(Update, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(Delete, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35))))
.addGroup(layout.createSequentialGroup()
.addGap(135, 135, 135)
.addComponent(jLabel1))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bokName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bookName))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(authorName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(autherName))
.addGap(29, 29, 29)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Delete)
.addComponent(Add)
.addComponent(Update))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)))
.addContainerGap())
);
pack();
}// </editor-fold>
private void AddMouseClicked(java.awt.event.MouseEvent evt) {
AddController addController = new AddController();
Book book = new Book();
book.setBookName(bokName.getText());
book.setAuthorName(authorName.getText());
addController.add(book);
defaultTableModel.addRow(new String[]{book.BookName, book.AuthorName});
//add value to table OR refresh table automatically
}
private void UpdateMouseClicked(java.awt.event.MouseEvent evt) {
UpdateController updateController = new com.company.librarySystem.bm.controller.UpdateController();
Book book = new Book();
book.setBookName(bokName.getText());
book.setAuthorName(authorName.getText());
updateController.update(book);
}
private void DeleteMouseClicked(java.awt.event.MouseEvent evt) {
DeleteController deleteController = new DeleteController();
Book book = new Book();
book.setBookName(bokName.getText());
deleteController.delete(book);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Add;
private javax.swing.JButton Delete;
private javax.swing.JButton Update;
private javax.swing.JLabel autherName;
private javax.swing.JTextField authorName;
private javax.swing.JTextField bokName;
private javax.swing.JLabel bookName;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JTable jTable1;
// End of variables declaration
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.company.librarySystem.bm.dto;
/**
*
* @author Administrator
*/
public class Book {
public String BookName,AuthorName;
public void setBookName(String bookName) {
this.BookName= bookName;
// System.out.println(this.bookName);
}
public void setAuthorName(String authorName) {
this.AuthorName = authorName;
//System.out.println(this.authorName);
}
public String getbookName() {
return BookName;
}
public String getAuthorName() {
return AuthorName;
}
public void print(){
System.out.println(BookName+" "+AuthorName);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.company.librarySystem.bm.dao;
import com.company.librarySystem.bm.dto.Book;
import com.company.librarySystem.bm.misc.Constants;
import com.company.librarySystem.bm.misc.DBUtil;
import com.mysql.jdbc.ResultSetMetaData;
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BookDAOImpl implements com.company.librarySystem.bm.dao.BookDAO {
com.company.librarySystem.bm.misc.Constants constants = new Constants();
public final String TABLE = "Book";
public final String BOOKNAME = "bookName";
public final String AUTHORNAME = "authorName";
public ResultSet result;
Constants c = new Constants();
public List<Book> selectAll() {
List<Book> bookList = new ArrayList<Book>();
try {
Connection connection = com.company.librarySystem.bm.misc.DBUtil.createConnection();
//read deisgn by contract
String query = "SELECT * FROM Book";
Statement DataRequest = (Statement) connection.createStatement();
result = DataRequest.executeQuery(query);
ResultSetMetaData rsmd = (ResultSetMetaData) result.getMetaData();
Book book = new Book();
while (result.next()) {
book.setBookName(result.getString(1));
book.setAuthorName(result.getString(2));
bookList.add(book);
}
} catch (SQLException ex) {
Logger.getLogger(BookDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return bookList;
}
public List<Book> insert(Book book) {
List<Book> bookList = new ArrayList<Book>();
Connection connection = com.company.librarySystem.bm.misc.DBUtil.createConnection();
try {
String inserString = "INSERT INTO " + TABLE + " (" + BOOKNAME + ", " + AUTHORNAME + ") values (?,?)";
PreparedStatement sPreparedStatement = connection.prepareStatement(inserString);
sPreparedStatement.setString(1, book.getbookName());
sPreparedStatement.setString(2, book.getAuthorName());
sPreparedStatement.executeUpdate();
book.setBookName(book.getbookName());
book.setAuthorName(book.getAuthorName());
bookList.add(book);
} catch (Exception e) {
e.printStackTrace();
}
DBUtil.closeConnection(connection);
return bookList;
}
public boolean deleteBook(Book book) {
Connection connection = com.company.librarySystem.bm.misc.DBUtil.createConnection();
try {
String inserString = " DELETE FROM " + TABLE + " WHERE " + BOOKNAME + " =? ";
PreparedStatement sPreparedStatement = connection.prepareStatement(inserString);
sPreparedStatement.setString(1, book.getbookName());
sPreparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
DBUtil.closeConnection(connection);
return true;
}
public boolean updateBook(Book book) {
Connection connection = com.company.librarySystem.bm.misc.DBUtil.createConnection();
try {
PreparedStatement sPreparedStatement = connection.prepareStatement("update book set authorName=? where bookName =?");
sPreparedStatement.setString(1, book.getAuthorName());
sPreparedStatement.setString(2, book.getbookName());
sPreparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
DBUtil.closeConnection(connection);
return true;
}
}