Olá galera!
Estou estudando java ja tem uns 2 meses e tals… Tenho um script em java que faz o login numa base de dados mssql atraves de odbc e outro script é a interface gráfica.
Gostaria de alguem pra me ajudar a adaptar os scripts para fazer 1 só.
Script que faz atenticação:
[code]
import java.io.IOException;
import java.sql.;
import java.util.;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Login3 {
public static void main( String args[] ) {
String url = "jdbc:odbc:Banco";
Connection connection = null;
Statement statement = null;
// Cria a conexão com o banco
try {
Class.forName( “sun.jdbc.odbc.JdbcOdbcDriver” );
connection = DriverManager.getConnection( url );
connection.setAutoCommit(false);
}
catch ( ClassNotFoundException cnfex ) {
System.err.println( “Driver JDBC/ODBC nao encontrado.” );
cnfex.printStackTrace();
System.exit( 1 );
}
catch ( SQLException sqlex ) {
System.err.println( “Falha na conexao.” );
sqlex.printStackTrace();
System.exit( 1 );
}
// Executa a conexão
try {
statement = connection.createStatement();
if(jNome.getText().equals("") || jSenha.getText().equals("")){
JOptionPane.showMessageDialog(null, “Por favor, entre com usuário e senha!”);
}
else {
try{
String sql = "Select * from login where usuario like ? and senha like ? ";
try{
statement = connection.createStatement();
/* dá certo se conecta.abreConexao() retornar Connection*/
pstm=statement.prepareStatement(sql);
pstm.setString(1,jNome.getText()); /* 1 quer dizer: set no 1º ?/
pstm.setString(2,jSenha.getText());/ 2 quer dizer: set no 2º ?/
rs=pstm.executeQuery(); / aqui ele executa e rs recebe o retorno*/
}catch (SQLException ex){
ex.printStackTrace();
}
/* rs=pstm.executeQuery() foi bem-sucedido, rs possui um objeto. Se não, continua null */
if(rs!=null){
JOptionPane.showMessageDialog(null, “Bom trabalho”);
}
else{
JOptionPane.showMessageDialog(null, “Usuário ou senha invalidos…”);
}
// Fecha a conexão com o banco
statement.close();
connection.close();
System.exit(0);
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
System.exit(1);
}
}
}[/code]
Script de interface grafica:
[code]
import javax.swing.;
import java.awt.event.;
public class Login2 extends JFrame implements ActionListener {
JLabel jLabel1 = new JLabel(“Nome”);
JLabel jLabel2 = new JLabel(“Senha”);
JTextField jNome = new JTextField(10);
JPasswordField jSenha = new JPasswordField(10);
JButton jBotao1 = new JButton(“Logar”);
JButton jBotao2 = new JButton(“Cadastrar”);
JPanel jPanel = new JPanel();
public Login2() {
super(“Controle de Usuarios”);
setContentPane(jPanel);
jPanel.add(jLabel1);
jPanel.add(jNome);
jPanel.add(jLabel2);
jPanel.add(jSenha);
jPanel.add(jBotao1);
jPanel.add(jBotao2);
jBotao1.addActionListener(this);
jBotao2.addActionListener(this);
setSize(350,100);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jBotao1)
JOptionPane.showMessageDialog(null,“Voce apertou o botao Logar”);
else
JOptionPane.showMessageDialog(null,“Voce clicou no botao cadastrar”);
}
public static void main(String args[]) {
Login2 a = new Login2();
}
}[/code]
Obrigado galera!!