0

I'm rather new to the world of Java, and i am not quite sure how to do this.

I have a MySQL class which extracts a table just fine, and prints it to the console. However, i need to do custom SQL queries from another class, and i can't figure out how to do that. Anyone who can help out?

Here's the mysql class:

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class mySQL {

    public static void main(String[] args) {

        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://host:port/database";
        String user = "user";
        String password = "password";

        try {

            con = DriverManager.getConnection(url, user, password);
            pst = con.prepareStatement("SELECT * FROM NFT_users");
            rs = pst.executeQuery();
            con = DriverManager.getConnection(url, user, password);

            while (rs.next()) {
                System.out.print(rs.getInt(1));
                System.out.print(": ");
                System.out.println(rs.getString(2));
            }

        } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(mySQL.class.getName());
                lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {

            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(mySQL.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
}
3
  • So what is the problem? Why can't you do the same thing with custom query? Go to JDBC Tutorial for more info. Commented Nov 30, 2012 at 20:11
  • @Patric Reck : can you further explain what you're trying to do? Commented Nov 30, 2012 at 20:11
  • I'm trying to make a small chat, where i have my main class Chat. Within this i need to do a loop creating a User object for each user and assign some values. What i don't know is how to make that reference and call a (so far non-existing?) method to do this. Commented Nov 30, 2012 at 20:13

1 Answer 1

1

Your problem is not about SQL, it is about software design.

Consider a class whose entire existence is about giving you access to data. It would do all the connection stuff (private void loadConnection), and then had methods like ArrayList<User> getUsers().

Then, when you need your list of Users, all you would do is just do myDatabaseObject.getUsers() or, in another situation, do myDatabaseObject.getOtherObjects(). But none of the rest of your code would know anything about SQL or connecting to the DB.


If you want to do more than very simple database access, you should consider learning Hibernate. Wikipedia link. If you've ever been reading through questions on this site and wondered what Hibernate is, it's exactly the tool that does this.

As far as how to do this yourself, just put the connection code in the constructor, save the connection to a field, and have the methods described above do the queries (preferably using PreparedStatement).

Sign up to request clarification or add additional context in comments.

3 Comments

I would just expand this a little farther and create a parent class that handles the connection info. A base class if you will (BaseSQLObject) and then create an object for each table that will extend this base object. The child object would know all of the sql for that table and how to execute it. You should probably also create an interface to enforce certain basic methods.
You are right, this is what i need to do. However, i don't know how to approach this, and i can't find any tutorials showing anything about the subject
Thank you! That was just what i needed to get going again :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.