Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MUDE AQUI E COLOQUE O PACOTE DE ACORDO COM O SEU PROJETO
- //DICA: OLHE O PACKAGE QUE OUTRA CLASSE DO SEU PROJETO EST� E COLOQUE O MESMO!
- //Classes necessárias para uso de Banco de dados //
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class ConexaoMySQL {
- private static ConexaoMySQL instance = null;
- private Connection connection = null;
- private ConexaoMySQL() {
- try {
- String driverName = "com.mysql.jdbc.Driver";
- Class.forName(driverName);
- String serverName = "127.0.0.1";
- //nesta linha estar� o nome do seu banco de dados. Olhe no workbench
- //o nome do banco. Caso voce tenha criado o modelo no workbench e n�o
- //tenha modificado o nome do banco, o nome padr�o � "mydb".
- String dbName = "bancodadoscursos";
- String url = "jdbc:mysql://" +
- serverName + "/" +
- dbName;
- //ajuste o username e password de acordo com o que voce utiliza para
- //acessar o banco
- String username = "root";
- String password = "123muda123";
- connection = DriverManager.getConnection(url,
- username, password);
- if (connection != null) {
- System.out.println("STATUS--->Conectado "
- + "com sucesso!");
- } else {
- System.err.println("STATUS--->Não foi "
- + "possivel realizar conexão");
- }
- connection.setAutoCommit(true);
- } catch (ClassNotFoundException e) {
- System.out.println("O driver expecificado"
- + " nao foi encontrado.");
- } catch (SQLException e) {
- System.out.println("Nao foi possivel"
- + " conectar ao Banco de Dados.");
- e.printStackTrace();
- }
- }
- public static ConexaoMySQL getInstance() {
- if (instance == null) {
- instance = new ConexaoMySQL();
- }
- return instance;
- }
- public java.sql.Connection getConnection() {
- return connection;
- }
- }
- import java.sql.Connection;
- import java.sql.SQLException;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Picasso
- */
- public class CadastroDisciplinasForm extends javax.swing.JFrame {
- private String cmd;
- private Object ConexaoMySQL;
- /**
- * Creates new form CadastroDisciplinasForm
- */
- public CadastroDisciplinasForm() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- jTextFieldNomeDisciplina = new javax.swing.JTextField();
- jTextFieldCargaHoraria = new javax.swing.JTextField();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jTextFieldCursoPertencente = new javax.swing.JTextField();
- jTextFieldNumeroVagas = new javax.swing.JTextField();
- jTextFieldPeriodo = new javax.swing.JTextField();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jLabel1.setText("Nome da Disciplina:");
- jLabel2.setText("Carga Horária:");
- jLabel3.setText("Curso Pertencente:");
- jLabel4.setText("Número de Vagas:");
- jLabel5.setText("Período:");
- jButton1.setText("Cancelar");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setText("Cadastrar");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(19, 19, 19)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel1)
- .addComponent(jLabel2))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jTextFieldNomeDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jTextFieldCargaHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel3)
- .addComponent(jLabel4)
- .addComponent(jLabel5))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(layout.createSequentialGroup()
- .addGap(16, 16, 16)
- .addComponent(jTextFieldCursoPertencente, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jTextFieldNumeroVagas)
- .addComponent(jTextFieldPeriodo))))))
- .addContainerGap(22, Short.MAX_VALUE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jButton2)
- .addGap(30, 30, 30)
- .addComponent(jButton1)
- .addGap(20, 20, 20))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(23, 23, 23)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(jTextFieldNomeDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(jTextFieldCargaHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel3)
- .addComponent(jTextFieldCursoPertencente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel4)
- .addComponent(jTextFieldNumeroVagas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel5)
- .addComponent(jTextFieldPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jButton1)
- .addComponent(jButton2))
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- dispose();
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- // TODO add your handling code here:
- String insert = "insert into disciplinas (NomeDisciplina, CargaHoraria, CursoPertencente, NumeroVagas, Periodo) VALUES ('"+
- jTextFieldNomeDisciplina.getText()+"', "+jTextFieldCargaHoraria.getText()+", '"+jTextFieldCursoPertencente.getText()+"', "+jTextFieldNumeroVagas.getText()+", "+jTextFieldPeriodo.getText()+"')";
- Connection conec = ConexaoMySQL.getInstance().getConnection();
- conec.createStatement().executeUpdate(insert);
- } catch (SQLException ex) {
- }
- }
- /**
- * @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(CadastroDisciplinasForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(CadastroDisciplinasForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(CadastroDisciplinasForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(CadastroDisciplinasForm.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 CadastroDisciplinasForm().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JTextField jTextFieldCargaHoraria;
- private javax.swing.JTextField jTextFieldCursoPertencente;
- private javax.swing.JTextField jTextFieldNomeDisciplina;
- private javax.swing.JTextField jTextFieldNumeroVagas;
- private javax.swing.JTextField jTextFieldPeriodo;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement