Advertisement
stevennathaniel

to_tsvector to_tsquery Berhasil Dijalankan Dengan Vector

May 7th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.66 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package latihan15;
  7.  
  8.  
  9. import javax.swing.SwingUtilities;
  10.  
  11. import javax.swing.JTable;
  12.  
  13. import java.sql.Connection;
  14.  
  15. import java.sql.DriverManager;
  16.  
  17. import java.sql.ResultSet;
  18.  
  19. import java.sql.ResultSetMetaData;
  20.  
  21. import java.sql.Statement;
  22.  
  23. import java.util.logging.Level;
  24.  
  25. import java.util.logging.Logger;
  26.  
  27. import javax.swing.table.DefaultTableModel;
  28.  
  29. import java.sql.*;
  30.  
  31. import java.util.Locale;
  32.  
  33. import java.util.Vector;
  34.  
  35.  
  36.  
  37.  
  38.  
  39. /**
  40.  *
  41.  * @author steven
  42.  */
  43. public class CariData2 extends javax.swing.JFrame {
  44.  
  45.     /**
  46.      * Creates new form CariData2
  47.      */
  48.     public CariData2() {
  49.         initComponents();
  50.     }
  51.  
  52.     /**
  53.      * This method is called from within the constructor to initialize the form.
  54.      * WARNING: Do NOT modify this code. The content of this method is always
  55.      * regenerated by the Form Editor.
  56.      */
  57.     @SuppressWarnings("unchecked")
  58.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  59.     private void initComponents() {
  60.  
  61.         jButton1 = new javax.swing.JButton();
  62.         jScrollPane1 = new javax.swing.JScrollPane();
  63.         jTable1 = new javax.swing.JTable();
  64.  
  65.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  66.  
  67.         jButton1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  68.         jButton1.setText("Cari");
  69.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  70.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  71.                 jButton1ActionPerformed(evt);
  72.             }
  73.         });
  74.  
  75.         jTable1.setModel(new javax.swing.table.DefaultTableModel(
  76.             new Object [][] {
  77.                 {null, null, null, null},
  78.                 {null, null, null, null},
  79.                 {null, null, null, null},
  80.                 {null, null, null, null}
  81.             },
  82.             new String [] {
  83.                 "Title 1", "Title 2", "Title 3", "Title 4"
  84.             }
  85.         ));
  86.         jScrollPane1.setViewportView(jTable1);
  87.  
  88.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  89.         getContentPane().setLayout(layout);
  90.         layout.setHorizontalGroup(
  91.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  92.             .addGroup(layout.createSequentialGroup()
  93.                 .addContainerGap()
  94.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  95.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 691, javax.swing.GroupLayout.PREFERRED_SIZE)
  96.                     .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
  97.                 .addContainerGap(23, Short.MAX_VALUE))
  98.         );
  99.         layout.setVerticalGroup(
  100.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  101.             .addGroup(layout.createSequentialGroup()
  102.                 .addContainerGap()
  103.                 .addComponent(jButton1)
  104.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  105.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
  106.                 .addGap(50, 50, 50))
  107.         );
  108.  
  109.         pack();
  110.     }// </editor-fold>                        
  111.  
  112.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  113.         // TODO add your handling code here:
  114.        
  115.        
  116.         Connection Koneksi = null;
  117.        
  118.         Statement stmt = null;
  119.        
  120.        
  121.         // String query = "select nik,namalengkap from istri";
  122.        
  123.         String cariData = "select nik,namalengkap from istri where to_tsvector(namalengkap) @@ to_tsquery('sri')";
  124.        
  125.         String url = "jdbc:postgresql://localhost:5432/pdam";
  126.        
  127.         String user = "steven";
  128.        
  129.         String password = "kucing";
  130.        
  131.        
  132.         try{
  133.            
  134.            
  135.             Koneksi = DriverManager.getConnection(url,user,password);
  136.            
  137.             stmt = Koneksi.createStatement();
  138.            
  139.             ResultSet rs = stmt.executeQuery(cariData);
  140.            
  141.             ResultSetMetaData rsmd = rs.getMetaData();
  142.            
  143.             Vector<Vector> coba2 = new Vector<Vector>();
  144.            
  145.             while(rs.next()){
  146.                
  147.                 Vector<String> isiBaris = new Vector<String>();
  148.                
  149.                 isiBaris.add(rs.getString("nik"));
  150.                
  151.                 isiBaris.add(rs.getString("namalengkap"));
  152.                
  153.                
  154.                 coba2.add(isiBaris);
  155.                
  156.                
  157.                 Vector<String> namaKolom = new Vector<String>();
  158.                
  159.                 namaKolom.addElement(rsmd.getColumnLabel(1));
  160.                
  161.                 namaKolom.addElement(rsmd.getColumnLabel(2));
  162.                
  163.                
  164.                 DefaultTableModel tabel1 = new DefaultTableModel(coba2,namaKolom);
  165.                
  166.                 jTable1.setModel(tabel1);
  167.                
  168.                 Koneksi.close();
  169.                
  170.                
  171.                
  172.             }
  173.            
  174.            
  175.         }catch (SQLException ex){
  176.            
  177.             ex.printStackTrace();
  178.            
  179.         }
  180.        
  181.     }                                        
  182.  
  183.     /**
  184.      * @param args the command line arguments
  185.      */
  186.     public static void main(String args[]) {
  187.         /* Set the Nimbus look and feel */
  188.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  189.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  190.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  191.          */
  192.         try {
  193.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  194.                 if ("Nimbus".equals(info.getName())) {
  195.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  196.                     break;
  197.                 }
  198.             }
  199.         } catch (ClassNotFoundException ex) {
  200.             java.util.logging.Logger.getLogger(CariData2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  201.         } catch (InstantiationException ex) {
  202.             java.util.logging.Logger.getLogger(CariData2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  203.         } catch (IllegalAccessException ex) {
  204.             java.util.logging.Logger.getLogger(CariData2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  205.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  206.             java.util.logging.Logger.getLogger(CariData2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  207.         }
  208.         //</editor-fold>
  209.  
  210.         /* Create and display the form */
  211.         java.awt.EventQueue.invokeLater(new Runnable() {
  212.             public void run() {
  213.                 new CariData2().setVisible(true);
  214.             }
  215.         });
  216.     }
  217.  
  218.     // Variables declaration - do not modify                    
  219.     private javax.swing.JButton jButton1;
  220.     private javax.swing.JScrollPane jScrollPane1;
  221.     private javax.swing.JTable jTable1;
  222.     // End of variables declaration                  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement