private void Simpan() { if (btnSave.getText().equals("Edit")) { try { Class.forName(KoneksiDatabase.driver); java.sql.Connection c = DriverManager.getConnection( KoneksiDatabase.database, KoneksiDatabase.user, KoneksiDatabase.pass); Statement s = c.createStatement(); String sql; sql = "update absensi_lembur set tipehari = '"; sql += cmbJenisHari.getSelectedItem() + "', tot_waktu_lembur='"; sql += txtTotalLembur.getText() + "'"; sql += "where kd_absen = '" + txtKodeAbsen.getText() + "'"; s.executeQuery(sql); JOptionPane.showMessageDialog( null, "Data Berhasil Disimpan!!!", "Informasi", JOptionPane.INFORMATION_MESSAGE); } catch (Exception e) { JOptionPane.showMessageDialog( null, "Gagal Disimpan, Data Harus Lengkap !!!", "Peringatan", JOptionPane.WARNING_MESSAGE); } } Baru(); }
private void btnDeleteActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btnDeleteActionPerformed // TODO add your handling code here: if (txtKodeAbsen.getText().equals("")) { JOptionPane.showMessageDialog( null, "Isi Kode Absen yang Akan dihapus !!!", "Peringatan", JOptionPane.ERROR_MESSAGE); txtKodeAbsen.requestFocus(); } else { try { Class.forName(KoneksiDatabase.driver); java.sql.Connection c = DriverManager.getConnection( KoneksiDatabase.database, KoneksiDatabase.user, KoneksiDatabase.pass); Statement s = c.createStatement(); String sql = "DELETE FROM absensi_lembur where kd_absen ='" + txtKodeAbsen.getText() + "'"; s.executeUpdate(sql); JOptionPane.showMessageDialog(null, "Data Berhasil Dihapus !"); Baru(); } catch (Exception e) { JOptionPane.showMessageDialog( null, "Kemungkinan terjadi kegagalan koneksi", "Warning", JOptionPane.ERROR_MESSAGE); } } } // GEN-LAST:event_btnDeleteActionPerformed
private void Baru() { btnSave.setText("Save"); txtNip.requestFocus(); txtNip.setText(""); try { Class.forName(KoneksiDatabase.driver); java.sql.Connection c = DriverManager.getConnection( KoneksiDatabase.database, KoneksiDatabase.user, KoneksiDatabase.pass); Statement s = c.createStatement(); String sql = "select * from absensi_lembur"; ResultSet rs = s.executeQuery(sql); final String[] headers = { "Kd Absen", "NIP", "Tgl Absen", "Masuk", "Pulang", "Hari", "Tipe Hari", "Terlambat", "Lembur", "Tipe Lembur", "Tot Lembur", "Tunj Makan", "Tunj Transport" }; rs.last(); int n = rs.getRow(); Object[][] data = new Object[n][13]; int p = 0; rs.beforeFirst(); while (rs.next()) { data[p][0] = rs.getString(1); data[p][1] = rs.getString(2); data[p][2] = rs.getString(3); data[p][3] = rs.getString(4); data[p][4] = rs.getString(5); data[p][5] = rs.getString(6); data[p][6] = rs.getString(7); data[p][7] = rs.getString(8); data[p][8] = rs.getString(9); data[p][9] = rs.getString(10); data[p][10] = rs.getString(11); data[p][11] = rs.getString(12); data[p][12] = rs.getString(13); p++; } tblLembur.setModel(new DefaultTableModel(data, headers)); tblLembur.setAlignmentX(CENTER_ALIGNMENT); } catch (Exception e) { JOptionPane.showMessageDialog( null, "Gagal Koneksi, Ada Kesalahan.", "Warning", JOptionPane.WARNING_MESSAGE); } }
public void initDB(String url) { try { db = DriverManager.getConnection(url); statement = db.createStatement(); } catch (Exception e) { System.out.println("Database could not started"); e.printStackTrace(); } } // initDB sonu
static { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql:///hospital", "root", "root"); pa = con.prepareStatement("insert into patient values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) "); pd = con.prepareStatement("delete from patient where pid=? "); pl = con.prepareStatement("select * from patient order by pid"); pps = con.prepareStatement("select * from patient where pid=? "); pps1 = con.prepareStatement("select * from patient where plnm= ?"); } catch (Exception e) { System.out.println(e); } }
// 采用静态初始化块来初始化Connection、Statement对象 static { try { Properties props = new Properties(); props.load(new FileInputStream("mysql.ini")); String drivers = props.getProperty("driver"); String url = props.getProperty("url"); String username = props.getProperty("user"); String password = props.getProperty("pass"); // 加载数据库驱动 Class.forName(drivers); // 取得数据库连接 conn = DriverManager.getConnection(url, username, password); stmt = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } }
public ItensVendidos() { super("Itens Vendidos"); JButton imprimir; final DefaultTableModel modelo = new DefaultTableModel(); // constrói a tabela JTable tabela = new JTable(modelo); // Cria duas colunas modelo.addColumn("Código"); modelo.addColumn("Vendedor"); modelo.addColumn("Produto"); modelo.addColumn("Quantidade"); imprimir = new JButton("Imprimir"); imprimir.setBounds(100, 450, 30, 24); // exibe os dados da tabela MySQL // Conexao banco = new Conexao(); String retorno = "erro"; try { // Connection ExConn = (Connection) banco.abrirBDconn(); Class.forName("com.mysql.jdbc.Driver"); Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost/banco", "root", ""); Statement stmt = conexao.createStatement(); // procedimentos para obter os dados de uma tabela String query = "SELECT * FROM venda2"; ResultSet rs = stmt.executeQuery(query); while (rs.next()) { int id = rs.getInt("idVendedor"); String nome = rs.getString("nomeVendedor"); String produto = rs.getString("produto"); int qtd = rs.getInt("qtd"); modelo.addRow(new Object[] {new Integer(id), nome, produto, new Integer(qtd)}); } // fim procedimento para obter os dados } catch (SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } catch (Exception e) { System.out.println("Problemas ao tentar conectar com o banco de dados"); } // fim MySQL tabela.setPreferredScrollableViewportSize(new Dimension(350, 50)); Container c = getContentPane(); c.setLayout(new FlowLayout()); JScrollPane scrollPane = new JScrollPane(tabela); c.add(scrollPane); c.add(imprimir); setSize(400, 300); setVisible(true); }
public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, login, clau); }