Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java连接到MySQL_Java_Mysql - Fatal编程技术网

使用Java连接到MySQL

使用Java连接到MySQL,java,mysql,Java,Mysql,好吧,我对java真的很陌生,我不知道如何真正做到这一点。如果你们能帮我的话,我可能会了解更多。我得到了我的主类Mysql.java和一个连接类Connection.java,现在我希望它使用Connection.java部分连接到数据库。但是我想从Mysql.java使用它。我现在就知道了: java import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import

好吧,我对java真的很陌生,我不知道如何真正做到这一点。如果你们能帮我的话,我可能会了解更多。我得到了我的主类Mysql.java和一个连接类Connection.java,现在我希望它使用Connection.java部分连接到数据库。但是我想从Mysql.java使用它。我现在就知道了:

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

我尝试在mysql.java中使用新连接,但我可能是个十足的白痴。如果你们能帮助我,那就太好了。

您可以在url字符串中对自己的主机和数据库名称使用以下代码。我已经测试过了

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

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

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

}

您需要在Mysql类的main方法中创建的连接实例上调用getConnection方法。您有一个名为Connection的类,并且您正试图在同一个类中使用java.sql.Connection。这可能会导致名称冲突-将您的类称为其他名称;这是错的吗?我不明白。我很抱歉,我使用新的msqlconn.java类获得了它,它显示在控制台中,但它在ClassNotFoundException上给出了一个错误。现在该怎么办?代码似乎正在运行,但每次我运行它时,控制台都会显示:com.mysql.jdbc.Driver我安装了它。为什么会这样说?我已经告诉过你在项目构建路径中包含一个JAR文件。请下载并在您的项目中包含“mysql-connector-java-5.1.23-bin.jar”文件。你可以在这里找到它。公共类Connectionn{public void getConnection抛出SQLException{Connection conn=null;重试{Class.forNamecom.mysql.jdbc.Driver;字符串url=jdbc:mysql://hostname:port/dbname;String user=username;String password=password;conn=DriverManager.getConnectionurl,user,password;System.out.printlconnection已建立;}catchClassNotFoundException e{}catchSQLException e{System.out.printlne.getMessage;System.exit0;}}首先创建Mysql类和exted连接类,在Mysql类中创建连接类对象,调用连接类方法getConnection和Mysql-connector-java-5.1.14-bin.jar添加到您的项目构建路径库中,然后尝试使用