Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.sql.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.table.*;
- public class DatabaseTable {
- public static JTable tableQuery(String userId){
- try{
- Connection connection = null;
- connection = DatabaseConnection.getConnection();
- String query = "SELECT * FROM movies WHERE userID = '" + userId +"'";
- PreparedStatement pst = connection.prepareStatement(query);
- ResultSet rs = pst.executeQuery(query);
- ResultSetMetaData md = rs.getMetaData();
- int columnNum = md.getColumnCount();
- String[] columns = new String[columnNum];
- for(int i = 0; i < columnNum; i++){
- columns[i] = md.getColumnName(i);
- }
- rs.last();
- int rows = rs.getRow();
- rs.first();
- Object[][] data = new Object[rows][columnNum];
- while(rs.next()){
- for(int j = 0; j < columnNum; j++){
- data[rs.getRow()][columnNum] = rs.getObject(j);
- }
- }
- JTable jt = new JTable(data, columns);
- return jt;
- }
- catch(Exception e){
- System.out.println("Database error.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement