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

我一直在尝试从我的 ubuntu 连接 mysql。我什至没有 JAVA 的基本概念。此代码从 mysql 站点复制。谁能帮我用一个简单的程序连接mysql。

4

1 回答 1

2

确保您的类路径中有 mysql 连接器 jar。这是您可以运行程序的方式:

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

于 2013-06-01T13:45:39.897 回答