import java.awt.event.*;
import java.sql.*;
public class JDBCInserta extends Frame implements ActionListener{
TextField texto;
Button b1;
TextArea data;
Label etiqueta;
int rows = 0;
public JDBCInserta(){
super("JDBC Inserta");
setSize(240, 280);
setLayout(new FlowLayout());
TextField("jdbc:mysql://127.0.0.1/test?
user=zaurus&password=", 20);
etiqueta = new Label("Escriba el texto a insertar:");
add(etiqueta);
texto=new TextField("algun texto", 18);
add(texto);
b1=new Button("Inserta Datos");
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 insertaDatos() throws SQLException{
String usr, pass;
String f1, f2;
String url="jdbc:mysql://127.0.0.1/test?
user=zaurus&password=";
String text1 = texto.getText();
PreparedStatement pstmt = null;
try{
// initialize & load driver
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("Could not find driver!");
System.exit(1);
}
Connection con=DriverManager.getConnection(url);//, usr, pass);
try {
pstmt = con.prepareStatement(
"INSERT into tablaPrueba ( "+
" identif,"+
" campoTexto)"+
" values ( "+
" '',"+
" ?) ");
}
catch (SQLException e) {
System.err.println(e);
}
pstmt.setString(1,text1);
rows = 0;
rows=pstmt.executeUpdate();
pstmt.close();
if( rows == 1 ) {
data.append("\nRegistro Insertado");
}
}
public void actionPerformed(ActionEvent e){
data.replaceRange("Field 1\t\tField 2",0,59);
data.append("\n-----\t\t-----");
try{
insertaDatos();
}catch(SQLException ex){
data.append("\nError inserting in database:"+rows+"inserted.");
data.append("\n"+ex.getMessage());
}
}
public static void main(String args[]){
new JDBCInserta();
}
}