import java.awt.event.*;
import java.sql.*;
public class JDBCTest extends Frame implements ActionListener{
TextField server, user, passwd;
Button b1;
TextArea data;
public JDBCTest(){
super("JDBC Test");
setSize(240, 280);
setLayout(new FlowLayout());
server=new TextField("jdbc:mysql://127.0.0.1/test?
user=zaurus&password=", 20);
add(server);
user=new TextField("zaurus", 8);
add(user);
passwd=new TextField("", 6);
passwd.setEchoChar('*');
add(passwd);
b1=new Button("Get Data");
b1.addActionListener(this);
add(b1);
data=new TextArea(10, 30);
add(data);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
show();
}
public void getData() throws SQLException{
String url, usr, pass;
String f1, f2, f3;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("Could not find driver!");
System.exit(1);
}
url=server.getText();
usr=user.getText();
pass=passwd.getText();
Connection con=DriverManager.getConnection(url, usr, pass);
Statement stm=con.createStatement();
ResultSet rs=stm.executeQuery("SELECT identif, campoTexto
FROM tablaPrueba");
while(rs.next()){
f1=rs.getString(1);
f2=rs.getString(2);
data.append("\n"+f1+"\t\t"+f2);
}
}
public void actionPerformed(ActionEvent e){
data.replaceRange("Field 1\t\tField 2",0,49);
data.append("\n-----\t\t-----");
try{
getData();
}catch(SQLException ex){
data.append("\nError connecting to database:");
data.append("\n"+ex.getMessage());
}
}
public static void main(String args[]){
new JDBCTest();
}
}