用Java连接MySQL

时间:2014-10-16 18:23:51

标签: java mysql

好吧我是java的新手,我不知道如何真正做到这一点。如果你们能帮助我,那么我可能会理解更多。我得到了我的主类(Mysql.java)和一个Connection类(Connection.java)现在我希望它使用Connection.java部分连接到数据库。但我想从Mysql.java中使用它。我现在明白了:

Mysql.java

public class Mysql {

    public static void main(String[] args) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = screenSize.width;
        JFrame frame = new MainFrame(w/5, 100, 2*w/5, 0);
        frame.show();
        new Connection();
    }


}
class MainFrame extends JFrame {
    MainFrame(int width, int height, int x, int y) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Todothelydoo - To do planner");
        setSize(width, height);
        setLocation(x, y);
        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(Color.blue);
    }
}

Connection.java

public class Connection {
    public static Connection getConnection() throws SQLException {
        Connection conn = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://h2318966.stratoserver.net/";
            String user = "root";
            String password = "";
            conn = (Connection) DriverManager.getConnection(url, user, password);
            System.out.println("Connection established");
        }
        catch(ClassNotFoundException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        return conn;
    }
}

我尝试在mysql.java中使用新的连接,但我可能是一个彻头彻尾的白痴。如果你们能帮助我那会很棒。

2 个答案:

答案 0 :(得分:0)

您可以在url字符串中将以下代码与您自己的主机和数据库名称一起使用。我测试了它。

注意:在项目中包含mysql-connector.jar

Mysql.java

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.sql.SQLException;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Mysql {

    public static void main(String[] args) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = screenSize.width;
        JFrame frame = new MainFrame(w/5, 100, 2*w/5, 0);
        frame.show();
        try {
            new Connection().getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
class MainFrame extends JFrame {
    MainFrame(int width, int height, int x, int y) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Todothelydoo - To do planner");
        setSize(width, height);
        setLocation(x, y);
        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(Color.blue);
    }
}

Connection.java

import java.sql.DriverManager;
import java.sql.SQLException;

public class Connection {
    public static java.sql.Connection getConnection() throws SQLException {
        java.sql.Connection conn = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost/database_name";
            String user = "root";
            String password = "";
            conn =  DriverManager.getConnection(url, user, password);
            System.out.println("Connection established");
        }
        catch(ClassNotFoundException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        return conn;
    }
}

答案 1 :(得分:0)

公共类Mysql扩展了Connectionn {

public static void main(String[] args) {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int w = screenSize.width;
    JFrame frame = new MainFrame(w/5, 100, 2*w/5, 0);
    frame.show();
     Connectionn con=new Connectionn();
     try {
        con.getConnection();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}