0
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;


    // Notice, do not import com.mysql.jdbc.*
    // or you will have problems!

    public class HelloWorld {
        public static void main(String[] args) {
            Connection conn = null;
        try {
            // The newInstance() call is a work around for some
            // broken Java implementations

            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            // handle the error
        }
        try {

                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","mypassword");

                // Do something with the Connection
        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
        }
    }

I have been trying to connect mysql from my ubuntu. I dont have even basic idea of JAVA. This code copied from mysql site. Can anyone help me to connect mysql with a simple program.

Shafeeque
  • 2,039
  • 2
  • 13
  • 28

1 Answers1

2

Make sure you have mysql connector jar in your classpath. This is how you can run your program:

java -cp /usr/local/jars/mysql-connector-java-5.1.8-bin.jar HelloWorld

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136