Created
October 13, 2014 20:34
-
-
Save Led-ds/7c973d4932e05205bc77 to your computer and use it in GitHub Desktop.
Criar Um
Formulario com a classe SWING,
Criar um SERVLET,
Criar uma Base de Dados.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexss.conexao; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
public class ConectJDBC { | |
private Connection conn; | |
private String user = "root"; | |
private String senha = ""; | |
private String url = "jdbc:mysql://localhost:3306/breadcrumb"; | |
private String drive = "com.mysql.jdbc.Driver"; | |
public void OpenConnect(){ | |
try{ | |
Class.forName(drive); | |
conn = DriverManager.getConnection(url,user,senha); | |
}catch(ClassNotFoundException ex){ | |
ex.printStackTrace(); | |
System.out.println("cannot found Driver Class" + ex); | |
}catch (SQLException e){ | |
e.printStackTrace(); | |
System.out.println("cannot found Service" + e); | |
} | |
} | |
public void CloseConnect(){ | |
try{ | |
conn.close(); | |
}catch (SQLException e){ | |
e.printStackTrace(); | |
System.out.println("Not Close Connection..."+ e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexss.daoContato; | |
import com.alexss.conexao.ConectJDBC; | |
import com.alexss.modelo.Contato; | |
import java.sql.*; | |
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
public class Negocio extends ConectJDBC{ | |
private Connection conn = null; | |
private Statement stmt = null; | |
private PreparedStatement pstmt = null; | |
private ResultSet rs = null; | |
private ConectJDBC conect; | |
private Calendar formtData; | |
public SimpleDateFormat novoFormato = null; | |
public Date data = null; | |
public boolean Save(Contato c){ | |
conect = new ConectJDBC(); | |
boolean conectado = false; | |
if(conect != null ){ | |
conect.OpenConnect(); | |
} | |
try { | |
data = formataData(c.getData()); | |
System.out.println(data); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
try{ | |
String query = "INSERT INTO contato(matricula, name, data)"+ | |
"VALUE (" + c.getMatricula() + ",'" + c.getNome() + "','" + data + "')"; | |
pstmt.execute(query); | |
conectado = true; | |
} | |
catch (SQLException exp){ | |
exp.printStackTrace(); | |
System.out.println("Not Execute your Query..." + exp); | |
} | |
return conectado; | |
} | |
public Date formataData(String data) throws ParseException { | |
if (data == null || data.equals("")) | |
return null; | |
Date dt = new Date(); | |
novoFormato = new SimpleDateFormat("dd/MM/yyyy"); | |
dt = novoFormato.parse(data); | |
System.out.println(novoFormato.format(dt)); | |
return dt; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexss.form; | |
import com.alexss.daoContato.Negocio; | |
import com.alexss.modelo.Contato; | |
import javax.swing.*; | |
import javax.swing.text.MaskFormatter; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.text.ParseException; | |
import java.util.Scanner; | |
public class FormMain extends JFrame implements ActionListener{ | |
private JTextField txtNome, txtMatricula; | |
private MaskFormatter maskData_Nascimento = null; | |
private JFormattedTextField jFormattedTextData = null; | |
private JLabel lblNome, lblMatricula, lblData; | |
private JButton btnCadastrar, btnConsultar; | |
public FormMain(){ | |
super("Contact"); | |
setSize(520,520); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
getContentPane().setLayout(null); | |
btnCadastrar = new JButton("Cadastrar"); | |
btnConsultar = new JButton("Consultar"); | |
lblNome = new JLabel("Nome:"); | |
lblMatricula = new JLabel("Matricula:"); | |
lblData = new JLabel("Data Inicio:"); | |
txtNome = new JTextField(); | |
txtMatricula = new JTextField(); | |
try{ | |
maskData_Nascimento = new MaskFormatter("##/##/####"); | |
}catch (ParseException e){ | |
System.out.println("Erro na Formatação" + e); | |
e.printStackTrace(); | |
} | |
getContentPane().add(btnCadastrar); | |
getContentPane().add(btnConsultar); | |
getContentPane().add(lblData); | |
getContentPane().add(lblMatricula); | |
getContentPane().add(lblNome); | |
getContentPane().add(txtNome); | |
getContentPane().add(txtMatricula); | |
jFormattedTextData = new JFormattedTextField(maskData_Nascimento); | |
getContentPane().add(jFormattedTextData); | |
//Montando a tela | |
lblNome.setBounds(5,60,100,20); | |
txtNome.setBounds(70,60,160,20); | |
lblMatricula.setBounds(5,100,120,20); | |
txtMatricula.setBounds(70,100,160,20); | |
lblData.setBounds(5,140,100,20); | |
jFormattedTextData.setBounds(70,140,160,20); | |
btnConsultar.setBounds(200,200,100,30); | |
btnCadastrar.setBounds(70,200,100,30); | |
lblNome.setForeground(Color.BLACK); | |
lblData.setForeground(Color.BLACK); | |
lblMatricula.setForeground(Color.BLACK); | |
txtNome.addActionListener(this); | |
txtMatricula.addActionListener(this); | |
btnCadastrar.addActionListener(this); | |
btnConsultar.addActionListener(this); | |
} | |
@Override | |
public void actionPerformed(ActionEvent action) { | |
if(action.getSource() == btnCadastrar){ | |
Contato contato = new Contato(); | |
Negocio ng = new Negocio(); | |
contato.setNome(txtNome.getText()); | |
contato.setMatricula(Integer.parseInt(txtMatricula.getText())); | |
contato.setData(jFormattedTextData.getText()); | |
boolean acesso = ng.Save(contato); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexss; | |
import com.alexss.form.FormMain; | |
import javax.swing.*; | |
import java.util.Scanner; | |
public class Main extends FormMain{ | |
Scanner sc = new Scanner(System.in); | |
public static void main(String[] args) { | |
FormMain form = new FormMain(); | |
form.setVisible(true); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.alexss.modelo; | |
public class Contato { | |
private int id; | |
private int matricula; | |
private String nome; | |
private String data; | |
public Contato(){ | |
} | |
public Contato(int _matric, String _nome, String _data){ | |
this.matricula = _matric; | |
this.nome = _nome; | |
this.data = _data; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public int getMatricula() { | |
return matricula; | |
} | |
public void setMatricula(int matricula) { | |
this.matricula = matricula; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getData() { | |
return data; | |
} | |
public void setData(String data) { | |
this.data = data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment