Insert+Update+Delete in one connections?
Hello.I'm new to Java,and i have one question:is this code good?
Code :
import java.sql.*;
public class Proizvod {
public static void main(String[] args) {
try {
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://localhost/test";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "root", "");
Statement st = conn.createStatement();
st.executeUpdate("INSERT INTO proizvod (id, naziv) " + "VALUES (006, 'browncake)");
String query = "delete from proizvod where id = ?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt(123, 222);
preparedStmt.execute();
String query = "update proizvod set naziv = ? where id = ?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt (221, 545);
preparedStmt.setString(2, "whitecandy");
preparedStmt.executeUpdate();
conn.close();
}
catch (Exception e)
}
}
Re: Insert+Update+Delete in one connections?
Define 'good'...does it work? What factors do you think might contribute to making it 'bad' or 'good'?
Re: Insert+Update+Delete in one connections?
Yes.Does it work?Can i use methods like is shown in code?
Re: Insert+Update+Delete in one connections?
Example:i have a database name base1,with one table(product) and data inside her:productid,name,price. How to create a class for that table product? Tnx in advance.
Maybe something like this?
public class Product {
private int id;
private String name;
private double price;
public Product(String name) {
this.name = name;
this.id = id;
}
private String getName() {
return name;
}
private String getId() {
return id;
}
private double getPrice() {
return price;
}
Re: Insert+Update+Delete in one connections?
Quote:
Originally Posted by
Hurricane
Hello.I'm new to Java,and i have one question:is this code good?
Code :
import java.sql.*;
public class Proizvod {
public static void main(String[] args) {
try {
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://localhost/test";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "root", "");
Statement st = conn.createStatement();
st.executeUpdate("INSERT INTO proizvod (id, naziv) " + "VALUES (006, 'browncake)");
String query = "delete from proizvod where id = ?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt(123, 222);
preparedStmt.execute();
String query = "update proizvod set naziv = ? where id = ?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt (221, 545);
preparedStmt.setString(2, "whitecandy");
preparedStmt.executeUpdate();
conn.close();
}
catch (Exception e)
}
}
//////////////////////
This is not a good java programming practice,because the concept of oop is not defined in the code.
So,group your insert,delete and update into different class.so that you can call them whenever you need them.
something like this.Hope u understand this.if not let me know.
Re: Insert+Update+Delete in one connections?
Tnx.Maybe something like this?
Code :
static void connect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/prodaja", "root", "");
try {
connect();
PreparedStatement ps = conn.prepareStatement("delete from proizvod where proizvod_id = ?");
ps.setInt(1,0010);
ps.executeUpdate();
ps.close();
}
try {
connect();
PreparedStatement ps = conn.prepareStatement("update proizvod set naziv = ? where id = ?");
ps.setString(1,"candy");
ps.setString(2,0011);
ps.executeUpdate();
ps.close();
}
try {
connect();
PreparedStatement ps = conn.prepareStatement("insert into proizvod(id, naziv) values(?,?)");
ps.setInt(1,0021);
ps.setString(2,"ball");
ps.executeUpdate();
ps.close();
}
conn.close();
}
catch (Exception ex) {