0

How can I connect to a MySQL DB and execute queries?

4
  • As a programming question, this should be moved to Stack Overflow. (Although I'm sure this has been asked there lots of times already...) Commented Jan 26, 2010 at 18:48
  • If you're just beginning, you might want to state that in your question. Also post what you've tried and what problems you've encountered. Otherwise you might not get any useful answers. Commented Jan 26, 2010 at 18:51
  • (I didn't find any exact duplicates though, with a quick look on SO.) Commented Jan 26, 2010 at 18:56
  • goggle "mysql java", click first match Commented Jan 27, 2010 at 9:52

4 Answers 4

5

First get your JDBC driver, such as mysql's Connector/J - add the jar to your project. Then, you can access a database like:

public class mysqlTest {

public static void main(String[] args) {
    String driver = "com.mysql.jdbc.Driver";
    String protocol = "jdbc:mysql://localhost/";
    String database = "database";
    String username = "mysqlUsername";
    String password = "mysqlPassword";

    Properties props = new Properties();
    props.put("user", username);
    props.put("password", password);

    Connection conn;
    Statement s;
    ResultSet rs;

    String createTableStatment = "CREATE TABLE test ( `id` INTEGER NOT NULL)";
    String insertStatment = "INSERT INTO test VALUES(1)";
    String selectQuery = "SELECT * FROM test";

    try {
        Class.forName(driver).newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        conn = DriverManager.getConnection(protocol + database,props);
        conn.setAutoCommit(true);
        s = conn.createStatement();
        s.execute(createTableStatment);
        s.execute(insertStatment);
        rs = s.executeQuery(selectQuery);
        while (rs.next()){
            System.out.println("id = "+rs.getString("id"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}

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

1 Comment

Fantastic answer Adam. You took this hobbyist Java dev from zero to MySQL query in under 15 minutes! And that includes .jar download/install! Uber kudos.
1

...if you really are going to use java to query a MySql server you should look further and read some about JDBC, ORM, transactions and how this concepts will help you using a database.

You can use JDBC to query an Mysql database, and virtually any other DBMS who have an JDBC driver implemented.

An ORM system provides the programmer with a layer between Objects and Database and makes all the hard work for you (in theory).

Even with the previous point you really need to have some basic knowledge about transactions, that encapsulates your actions in one atomic unit that only executes completely or does not execute at all.

2 Comments

Stupid answer. You don't add any value, and moreover you think you are funny with it. Questions from StackOverflow are in the first pages in Google searches. The point is to have actually an answer, not another google search. If you don't have anything to answer, then just say nothing. See this question on the meta site for more information: meta.stackexchange.com/questions/8724/…
Much better indeed. Thanks for improving that answer.
1

jdbc-mysql

Comments

0

You can try MySQL JDBC Utilities API for MySQL connectivity in java.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.