简体   繁体   English

Java到MySQL连接错误

[英]Java to MySQL connection error

I am still relatively new to programming and this is my first attempt to create a program with a database.我对编程还是比较陌生,这是我第一次尝试使用数据库创建程序。 I created the database in PhpMyAdmin.我在 PhpMyAdmin 中创建了数据库。 There is no errors between the two classes used to access the database but it does not print the required info.用于访问数据库的两个类之间没有错误,但它不打印所需的信息。 Just wondering if anyone more experienced then me can see where I have gone wrong.只是想知道是否有比我更有经验的人能看出我哪里出错了。 Thanks谢谢

The Main method is as follows: Main方法如下:

public class Main {
    public static void main(String[] args) {
        DBConnect connect = new DBConnect();
        connect.getData();
    }
}

Then the DBConnect method is as follows:那么DBConnect方法如下:

import java.sql.*;

public class DBConnect {
    private Connection con;
    private Statement st;
    private ResultSet rs;

    public DBConnect(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:8080/software","root","usbw");
            st = con.createStatement();
        }catch(Exception ex){
            System.out.println("Error:" + ex);
        }
    }

    public void getData(){
        try{
            String query="select from * football";
            rs = st.executeQuery(query);
            System.out.print("Records from the database");
            while(rs.next()){
                String name = rs.getString("Name");
                System.out.print("Name: "+name+" ");
            }
        }catch(Exception ex){
            System.out.println(ex);
        }
    }
}

If anyone can see where the problem lies or has any other sort of advice it would be greatly appreciated如果有人能看到问题所在或有任何其他建议,将不胜感激

thanks again再次感谢

You have incorrect query您的查询不正确

select from * football从 * 足球中选择

It should be它应该是

select * from football从足球中选择 *

Also check, that you have correct mysql port.还要检查您是否拥有正确的 mysql 端口。 I guess it should be 3306, but not 8080.我想应该是 3306,而不是 8080。

Try this:尝试这个:

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/software", "root", "usbw");
    Statement st = con.createStatement();
    String query="select * from football";
    ResultSet rs = st.executeQuery(query);
    System.out.print("Records from the database");
    while(rs.next()){
        String name = rs.getString("Name");
        System.out.print("Name: "+name+" ");
    }
}

Your SQL sentence is select from * football , and should be select * from football .你的 SQL 语句是select from * football ,应该是select * from football

Another thing, you are connecting to mysql using 8080 port, is that correct ??另一件事,您正在使用 8080 端口连接到 mysql,对吗? Generally MySQL listen in 3306 port.一般MySQL监听3306端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM