Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Insert+Update+Delete in one connections?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Insert+Update+Delete in one connections?

    Hello.I'm new to Java,and i have one question:is this code good?

    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)
     
                }
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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'?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Insert+Update+Delete in one connections?

    Yes.Does it work?Can i use methods like is shown in code?

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    }
    Last edited by Hurricane; February 24th, 2012 at 07:46 AM.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Insert+Update+Delete in one connections?

    Quote Originally Posted by Hurricane View Post
    Hello.I'm new to Java,and i have one question:is this code good?

    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.
    Last edited by teesoft; February 27th, 2012 at 03:36 AM.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Insert+Update+Delete in one connections?

    Tnx.Maybe something like this?

    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) {
    Last edited by Hurricane; February 27th, 2012 at 11:38 AM.

Similar Threads

  1. jdbc connections
    By rlk in forum JDBC & Databases
    Replies: 4
    Last Post: January 16th, 2012, 09:25 AM
  2. update query is firing first then insert query
    By salmondavid88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2011, 10:15 AM
  3. Replies: 0
    Last Post: January 12th, 2011, 08:41 AM
  4. Socket connections and Unsigned values
    By helloworld922 in forum Java Networking
    Replies: 5
    Last Post: June 15th, 2010, 08:48 PM
  5. How can i create fake IP addresses to extract information for the DB?
    By neomancer in forum Java Theory & Questions
    Replies: 4
    Last Post: May 8th, 2009, 04:54 AM