9 Replies Latest reply: Mar 16, 2007 7:26 PM by 807606 RSS

    Read from MySql with Java?

    807606
      Hi everybody,
      Another Java question from me on a different subject--

      The question is, is there a way to read data from a mySql database into a Java program and then do something with it?

      I've heard of the JDBC before, but I'm kind of confused about what it is and how to use it (would it be used for this?), so basically I know the answer to my question is yes, but I don't know where to start to make that work--can anyone point me in the right direction?

      Thanks as always,
      Jezzica85
        • 1. Re: Read from MySql with Java?
          807606
          http://dev.mysql.com/downloads/connector/j/3.1.html
          • 2. Re: Read from MySql with Java?
            807606
            You can read this article:

            http://java.sun.com/docs/books/tutorial/jdbc/overview/index.html

            package uk.co.jemos.experiments;
            
            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.PreparedStatement;
            import java.sql.ResultSet;
            import java.sql.SQLException;
            
            public class MySqlClient {
            
                 //Loads the driver
                 static {
                      try {
                           Class.forName("com.mysql.jdbc.Driver");
                      } catch (ClassNotFoundException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                      }
                 }
                 /**
                  * @param args
                  */
                 public static void main(String[] args) {
                      new MySqlClient().go();
            
                 }     
                      
                 private void go() {
                      
                      //The object encapsulating your connection to the db
                      Connection conn = null;
                      
                      //Your SQL string (? identify parameters)
                      final String sql = "SELECT ID FROM TEST WHERE VAR = ?";
                      
                      //The object encapsulating your SQL statement
                      PreparedStatement ps = null;
                      
                      //The result of your query
                      ResultSet rs = null;
                      
                      try {
                           
                           //Get a connection
                           conn = DriverManager.getConnection("jdbc:mysql://myserver:3306/test", 
                                                                      "user", 
                                                                      "password");
                           
                           //Create the prepared statement out your connection
                           ps = conn.prepareStatement(sql);
                           
                           //Set your parameter
                           ps.setInt(1, 1);
                           
                           //Get the data
                           rs = ps.executeQuery();
                           
                           //Do something with your data
                           while (rs.next()) {
                                System.out.println(rs.getInt("ID"));
                           }
                           
                      } catch (SQLException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                           
                      //Always ensure that you close the connection when finished!
                      } finally {
                           if (null != conn) {
                                try {
                                     conn.close();
                                } catch (SQLException e) {
                                     // TODO Auto-generated catch block
                                     e.printStackTrace();
                                }
                           }
                      }
                 }
            
            }
            • 3. Re: Read from MySql with Java?
              807606
              Thanks to both of you for your help so far, I think I'm starting to understand.

              So, basically what I'm getting from this is, I can use java.sql and javax.sql, and they're already installed in Java 5.0 so we can use the online APIs?

              I thought you had to download something else, please correct me if I'm wrong.

              Jezzica85
              • 4. Re: Read from MySql with Java?
                807606
                Thanks to both of you for your help so far, I think
                I'm starting to understand.

                So, basically what I'm getting from this is, I can
                use java.sql and javax.sql, and they're already
                installed in Java 5.0 so we can use the online APIs?

                I thought you had to download something else, please
                correct me if I'm wrong.

                Jezzica85
                Follow the link that I gave you to download the driver for MySql.
                • 5. Re: Read from MySql with Java?
                  807606
                  The JDK/JRE you downloaded when you installed Java is (database) vendor
                  independent. it includes those JDBC packages, but for each database vendor
                  (like MySQL, Oracle, etc...) you need their driver code which fits between
                  the JDBC and their database server. Reply #1 was a link to a download page
                  for the MySQL driver jar.
                  • 6. Re: Read from MySql with Java?
                    807606
                    OK, so then that means that I have to download that driver, then put it in the same place on my computer as my Java .jar files to make it work, and then I can access mySql by writing a Java program?
                    • 7. Re: Read from MySql with Java?
                      807606
                      Here's where people start having problems...

                      The driver jar can be placed anywhere, but your program must be able to find it!
                      If your program is zipped up into a jar, that jar's manifest needs an
                      entry to identify the location of this jar.

                      Here is the tutorial on that: http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html
                      • 8. Re: Read from MySql with Java?
                        807606
                        If you deploy in a server (let's say an application server), generally a server provides a place where you can put your jar dependencies (in this case the mysql jdbc driver). This is because the server writers generally create some sort of classloader that then they make available to your application. However is you are running your application as standalone solution you should have it in the classpath, and the manifest solution is great.
                        • 9. Re: Read from MySql with Java?
                          807606
                          Uh oh, I don't have a UNIX system, I have a Windows system.

                          I have Eclipse 3.2, and I can see what the path is to my jar files using that application--does all this mean that I have to unzip this file that I got from the website, then put that jar into one of the other jar files I already have? How do you do that?

                          Sorry, but I'm really confused now!