├── DBConnect.java ├── Department.form ├── Department.java ├── Javatask.java └── ResultSet.java /DBConnect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 | */ 5 | package javatask; 6 | import java.sql.*; 7 | import java.sql.ResultSet; 8 | /** 9 | * 10 | * @author balak 11 | */ 12 | public class DBConnect { 13 | public static void main(String args[]){ 14 | try{ 15 | //step1 load the driver class 16 | Class.forName("com.mysql.cj.jdbc.Driver"); 17 | //step2 create the connection object 18 | Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/department", "root","Bala@9344"); 19 | //step3 create the statement object 20 | Statement stmt=con.createStatement(); 21 | 22 | //step4 execute query 23 | //ResultSet rs=stmt.executeQuery("create table employee (empid number(4), empname varchar2(25))"); 24 | //ResultSet rs=stmt.executeQuery("insert into employee values(1002, 'Rajesh')"); 25 | // stmt.executeUpdate("update employee set empname=‘Aravind' where empid=120"); 26 | ResultSet rs=stmt.executeQuery("select * from dep_info"); 27 | while(rs.next()) 28 | System.out.println(rs.getInt(1)+" "+rs.getString(2)); 29 | //ResultSet rs=stmt.executeQuery("delete from employee"); 30 | //step5 close the connection object 31 | con.close(); 32 | }catch(Exception e){ System.out.println(e); 33 | 34 | } 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Department.form: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 |
125 |
126 | 127 | 128 | 129 | 130 | <Editor/> 131 | <Renderer/> 132 | </Column> 133 | <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> 134 | <Title/> 135 | <Editor/> 136 | <Renderer/> 137 | </Column> 138 | </TableColumnModel> 139 | </Property> 140 | <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> 141 | <TableHeader reorderingAllowed="true" resizingAllowed="true"/> 142 | </Property> 143 | </Properties> 144 | </Component> 145 | </SubComponents> 146 | </Container> 147 | </SubComponents> 148 | </Container> 149 | <Container class="javax.swing.JPanel" name="jPanel3"> 150 | <Properties> 151 | <Property name="background" type="java.awt.Color" editor="org.netbeans.beaninfo.editors.ColorEditor"> 152 | <Color blue="ff" green="cc" red="cc" type="rgb"/> 153 | </Property> 154 | </Properties> 155 | 156 | <Layout> 157 | <DimensionLayout dim="0"> 158 | <Group type="103" groupAlignment="0" attributes="0"> 159 | <Group type="102" alignment="0" attributes="0"> 160 | <EmptySpace min="-2" pref="20" max="-2" attributes="0"/> 161 | <Component id="list_dep" min="-2" max="-2" attributes="0"/> 162 | <EmptySpace max="32767" attributes="0"/> 163 | </Group> 164 | </Group> 165 | </DimensionLayout> 166 | <DimensionLayout dim="1"> 167 | <Group type="103" groupAlignment="0" attributes="0"> 168 | <Group type="102" alignment="0" attributes="0"> 169 | <EmptySpace min="-2" pref="107" max="-2" attributes="0"/> 170 | <Component id="list_dep" min="-2" max="-2" attributes="0"/> 171 | <EmptySpace max="32767" attributes="0"/> 172 | </Group> 173 | </Group> 174 | </DimensionLayout> 175 | </Layout> 176 | <SubComponents> 177 | <Component class="javax.swing.JButton" name="list_dep"> 178 | <Properties> 179 | <Property name="text" type="java.lang.String" value="List all"/> 180 | </Properties> 181 | <Events> 182 | <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="list_depActionPerformed"/> 183 | </Events> 184 | </Component> 185 | </SubComponents> 186 | </Container> 187 | </SubComponents> 188 | </Form> 189 | -------------------------------------------------------------------------------- /Department.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template 4 | */ 5 | package javatask; 6 | 7 | import java.sql.*; 8 | import javax.swing.table.DefaultTableModel; 9 | 10 | 11 | 12 | /** 13 | * 14 | * @author balak 15 | */ 16 | public class Department extends javax.swing.JFrame { 17 | 18 | /** 19 | * Creates new form Department 20 | */ 21 | public Department() { 22 | initComponents(); 23 | } 24 | 25 | /** 26 | * This method is called from within the constructor to initialize the form. 27 | * WARNING: Do NOT modify this code. The content of this method is always 28 | * regenerated by the Form Editor. 29 | */ 30 | @SuppressWarnings("unchecked") 31 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 32 | private void initComponents() { 33 | 34 | jPanel1 = new javax.swing.JPanel(); 35 | jLabel1 = new javax.swing.JLabel(); 36 | jPanel2 = new javax.swing.JPanel(); 37 | jScrollPane1 = new javax.swing.JScrollPane(); 38 | table_dep = new javax.swing.JTable(); 39 | jPanel3 = new javax.swing.JPanel(); 40 | list_dep = new javax.swing.JButton(); 41 | 42 | setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 43 | 44 | jPanel1.setBackground(new java.awt.Color(102, 255, 255)); 45 | 46 | jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 18)); // NOI18N 47 | jLabel1.setText("M Kumarasamy college of engineering"); 48 | 49 | javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 50 | jPanel1.setLayout(jPanel1Layout); 51 | jPanel1Layout.setHorizontalGroup( 52 | jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 53 | .addGroup(jPanel1Layout.createSequentialGroup() 54 | .addGap(124, 124, 124) 55 | .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 56 | .addGap(152, 152, 152)) 57 | ); 58 | jPanel1Layout.setVerticalGroup( 59 | jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 60 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 61 | .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 62 | .addComponent(jLabel1) 63 | .addGap(280, 280, 280)) 64 | ); 65 | 66 | jPanel2.setBackground(new java.awt.Color(153, 204, 255)); 67 | 68 | table_dep.setModel(new javax.swing.table.DefaultTableModel( 69 | new Object [][] { 70 | {null, null}, 71 | {null, null}, 72 | {null, null}, 73 | {null, null} 74 | }, 75 | new String [] { 76 | "dep_code", "dep_name" 77 | } 78 | )); 79 | jScrollPane1.setViewportView(table_dep); 80 | 81 | javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 82 | jPanel2.setLayout(jPanel2Layout); 83 | jPanel2Layout.setHorizontalGroup( 84 | jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 85 | .addComponent(jScrollPane1) 86 | ); 87 | jPanel2Layout.setVerticalGroup( 88 | jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 89 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE) 90 | ); 91 | 92 | jPanel3.setBackground(new java.awt.Color(204, 204, 255)); 93 | 94 | list_dep.setText("List all"); 95 | list_dep.addActionListener(new java.awt.event.ActionListener() { 96 | public void actionPerformed(java.awt.event.ActionEvent evt) { 97 | list_depActionPerformed(evt); 98 | } 99 | }); 100 | 101 | javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); 102 | jPanel3.setLayout(jPanel3Layout); 103 | jPanel3Layout.setHorizontalGroup( 104 | jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 105 | .addGroup(jPanel3Layout.createSequentialGroup() 106 | .addGap(20, 20, 20) 107 | .addComponent(list_dep) 108 | .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 109 | ); 110 | jPanel3Layout.setVerticalGroup( 111 | jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 112 | .addGroup(jPanel3Layout.createSequentialGroup() 113 | .addGap(107, 107, 107) 114 | .addComponent(list_dep) 115 | .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 116 | ); 117 | 118 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 119 | getContentPane().setLayout(layout); 120 | layout.setHorizontalGroup( 121 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 122 | .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 123 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 124 | .addContainerGap() 125 | .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 126 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 127 | .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 128 | ); 129 | layout.setVerticalGroup( 130 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 131 | .addGroup(layout.createSequentialGroup() 132 | .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) 133 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 134 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 135 | .addGroup(layout.createSequentialGroup() 136 | .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 137 | .addGap(0, 0, Short.MAX_VALUE)) 138 | .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 139 | .addContainerGap()) 140 | ); 141 | 142 | pack(); 143 | }// </editor-fold>//GEN-END:initComponents 144 | 145 | private void list_depActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_list_depActionPerformed 146 | // TODO add your handling code here: 147 | DefaultTableModel model = (DefaultTableModel) table_dep.getModel(); 148 | // DefaultTableModel model = {DefaultTableModel} table_dep.getModel(); 149 | while (model.getRowCount()>0){ 150 | for(int i=0;i<model.getRowCount();i++){ 151 | model.removeRow(i); 152 | } 153 | } 154 | try{ 155 | //step1 load the driver class 156 | Class.forName("com.mysql.cj.jdbc.Driver"); 157 | //step2 create the connection object 158 | Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/department", "root","Bala@9344"); 159 | //step3 create the statement object 160 | Statement stmt=con.createStatement(); 161 | 162 | //step4 execute query 163 | //ResultSet rs=stmt.executeQuery("create table employee (empid number(4), empname varchar2(25))"); 164 | //ResultSet rs=stmt.executeQuery("insert into employee values(1002, 'Rajesh')"); 165 | // stmt.executeUpdate("update employee set empname=‘Aravind' where empid=120"); 166 | java.sql.ResultSet rs=stmt.executeQuery("select * from dep_info"); 167 | while(rs.next()){ 168 | Object[] data={rs.getInt(1),rs.getString(2)}; 169 | model.addRow(data); 170 | } 171 | //ResultSet rs=stmt.executeQuery("delete from employee"); 172 | //step5 close the connection object 173 | con.close(); 174 | }catch(Exception e){ System.out.println(e); 175 | 176 | } 177 | 178 | }//GEN-LAST:event_list_depActionPerformed 179 | 180 | /** 181 | * @param args the command line arguments 182 | */ 183 | public static void main(String args[]) { 184 | /* Set the Nimbus look and feel */ 185 | //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 186 | /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 187 | * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 188 | */ 189 | try { 190 | for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 191 | if ("Nimbus".equals(info.getName())) { 192 | javax.swing.UIManager.setLookAndFeel(info.getClassName()); 193 | break; 194 | } 195 | } 196 | } catch (ClassNotFoundException ex) { 197 | java.util.logging.Logger.getLogger(Department.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 198 | } catch (InstantiationException ex) { 199 | java.util.logging.Logger.getLogger(Department.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 200 | } catch (IllegalAccessException ex) { 201 | java.util.logging.Logger.getLogger(Department.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 202 | } catch (javax.swing.UnsupportedLookAndFeelException ex) { 203 | java.util.logging.Logger.getLogger(Department.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 204 | } 205 | //</editor-fold> 206 | //</editor-fold> 207 | 208 | /* Create and display the form */ 209 | java.awt.EventQueue.invokeLater(new Runnable() { 210 | public void run() { 211 | new Department().setVisible(true); 212 | } 213 | }); 214 | } 215 | 216 | // Variables declaration - do not modify//GEN-BEGIN:variables 217 | private javax.swing.JLabel jLabel1; 218 | private javax.swing.JPanel jPanel1; 219 | private javax.swing.JPanel jPanel2; 220 | private javax.swing.JPanel jPanel3; 221 | private javax.swing.JScrollPane jScrollPane1; 222 | private javax.swing.JButton list_dep; 223 | private javax.swing.JTable table_dep; 224 | // End of variables declaration//GEN-END:variables 225 | } 226 | -------------------------------------------------------------------------------- /Javatask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template 4 | */ 5 | package javatask; 6 | 7 | /** 8 | * 9 | * @author balak 10 | */ 11 | public class Javatask { 12 | 13 | /** 14 | * @param args the command line arguments 15 | */ 16 | public static void main(String[] args) { 17 | // TODO code application logic here 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /ResultSet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 | */ 5 | package javatask; 6 | 7 | /** 8 | * 9 | * @author balak 10 | */ 11 | class ResultSet { 12 | 13 | boolean next() { 14 | throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody 15 | } 16 | 17 | String getInt(int i) { 18 | throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody 19 | } 20 | 21 | String getString(int i) { 22 | throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody 23 | } 24 | 25 | } 26 | --------------------------------------------------------------------------------