Problems with PreparedStatement
Hello all!
I'm having problems with the PreparedStatement in my class.. This method is going to insert data into the DB with parameters passed from another class. This is the error that Netbeans provides when i compile :
-
symbol : method PreparedStatement(java.lang.String)
location: interface java.sql.Connection
PreparedStatement stmt = con.PreparedStatement("INSERT INTO dbo.xxx (Nombre, Apellido) values('"+ Nombre +"', '"+ Apellido +"')");
1 error
--
publicvoid insertar(String Nombre, String Apellido){
Connection con;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// Llamando al driver, La palabra Class Va en mayuscula.
}
catch(Exception e){
System.out.println("No se pudo lograr la conexion");
return;
}
try{
con = DriverManager.getConnection("jdbc:odbc:con_sql","","" );
PreparedStatement stmt = con.PreparedStatement("INSERT INTO dbo.xxx (Nombre, Apellido) values('"+ Nombre +"', '"+ Apellido +"')");
}
catch(Exception e){
System.out.println("Error al insertar empleado" +" " + e);
}
}
# 1
Can you spot the difference between
PreparedStatement stmt = con.PreparedStatement("INSERT INTO dbo.xxx (Nombre, Apellido) values('"+ Nombre +"', '"+ Apellido +"')");
and
PreparedStatement stmt = con.prepareStatement("INSERT INTO dbo.xxx (Nombre, Apellido) values('"+ Nombre +"', '"+ Apellido +"')");
Manuel Leiria
# 2
instead of
PreparedStatement stmt = con.prepareStatement("INSERT INTO dbo.xxx (Nombre, Apellido) values('"+ Nombre +"', '"+ Apellido +"')");
why not do it like this
String sql = "insert into dbo.xxx (Nombre, Apellido) values(?,?)";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1,"Nombre");
stmt.setString(2,"Apellido");
stmt.executeUpdate();
# 3
nop it does not compile that way either...
By the way, can anyone tell me the difference between Statement and PreparedStatement? cuz when i use Statement it compiles just fine.. and works just fine.. but it throws a message saying "No resultSet found"
# 4
is the use of execute query right there?
public void insertar(String Nombre, String Apellido){
Connection con;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){
System.out.println("No se pudo lograr la conexion");
return;
}
try{
con = DriverManager.getConnection("jdbc:odbc:con_sql","","" );
PreparedStatement stmt = con.executeQuery("INSERT INTO dbo.xxx (Nombre, Apellido) values('"+ Nombre +"', '"+ Apellido +"')");
}
catch(Exception e){
System.out.println("Error al insertar empleado" + " " + e);
}
}
# 5
You are seemingly inventing method names. Please consult the API documentation for java.sql.Connection in future.
The answer is that your code should look like this.
PreparedStatement ps = connectionVariable.prepareStatement(stringWithSql);
ps.executeUpdate();
But as previously mentioned you are using PreparedStatements incorrectly. Please learn to use PreparedStatements correctly in the manner proscribed by a previous poster.
# 6
Thanks cotton the insert its working just fine.. Now one more question.. does the UPDATE sentence need a Prepared Statement as well? or can i use Statement ?
# 7
> Thanks cotton the insert its working just fine.. Now
> one more question.. does the UPDATE sentence need a
> Prepared Statement as well? or can i use Statement ?
You can use either Statement or PreparedStatement.
You should ALWAYS use PreparedStatements. But you MUST use them properly.
Please read this http://www.sdnshare.com/view.jsp?id=525
# 8
May I suggest you the JDBC tutorial? It's informative.
http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html
# 9
Hello again :-/
Ok now my problem is with the Select Statement.. I'm having problems while i try to pass the values i get from the database, to the textbox i have in another class.
I'm going to post both methods: one that gets the parameter ID to go look in the DB, and the other one that passes that ID and also is going to fill the text box with the info.
Ok this method takes the ID parameter, and then goes to the DB to get the data.
public void seleccion(int z, String Nombre, String Apellido){
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){
System.out.println("No se pudo lograr la conexion");
}
try{
con = DriverManager.getConnection("jdbc:odbc:con_sql","","");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT Nombre, Apellido FROM dbo.xxx WHERE ID=" + z);
while(rs.next()){
String Nom = rs.getString("Nombre");
String Ape = rs.getString("Apellido");
Formulario f = new Formulario();
f.setName(Nombre);
f.setName(Apellido);
}
}
catch(Exception e){
System.out.println("Error al seleccionar Empleado" + " "+ e);
}
}
private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {
String x = txtID.getText();
int z = Integer.parseInt(x);
Conexion c = new Conexion();
c.seleccion(z);
String txtNombre = c.seleccion(Nombre);
String txtApellido = c.seleccion(Apellido);
}
# 10
> Ok now my problem is with the Select Statement.. I'm
> having problems while i try to pass the values i get
> from the database, to the textbox i have in another
> class.
It would help if you explained what the problem was.