没有为jdbc找到合适的驱动程序:mysql:// localhost:3306 / test

时间:2013-06-01 13:40:39

标签: java mysql

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

我一直在尝试从我的ubuntu连接mysql。我甚至没有JAVA的基本概念。此代码从mysql站点复制。任何人都可以帮助我将mysql与一个简单的程序连接起来。

1 个答案:

答案 0 :(得分:2)

确保在类路径中有mysql连接器jar。这就是你运行程序的方法:

  

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