简体   繁体   English

首先尝试mysql连接问题

[英]mysql connection issue in first try

I have been learning java for about 3 weeks. 我已经学习了大约3周的java。 I wanted to connect mysql with java and I'm trying now. 我想用mysql连接mysql,我现在正在尝试。 But there is a problem. 但有一个问题。 I have already installed php,apache,mysql three in for my ubuntu. 我已经为我的ubuntu安装了php,apache,mysql三个。 And I can reach phpmyadmin from port 80 . 我可以从port 80到达phpmyadmin。 I think there is a problem when connecting database this is why I said port. 我认为连接数据库时出现问题,这就是我说port的原因。

Here is the my table: 这是我的表:

+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(2)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(32) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

And here is the java code: 这是java代码:

import java.sql.DriverManager;
import java.sql.Statement;

public class Main {

    public static void main(String args[]) {
        try {

            java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:80/test", "root", "1");

            Statement state = (Statement) conn.createStatement();
            String name = "try";
            state.executeUpdate("INSERT INTO deneme (name) VALUES (" + name + ")");

        } catch (Exception e) {
        }
    }
}

This code runs about 20 seconds and returns BUILD SUCCESSFUL (total time: 21 seconds) What is your idea about this problem? 此代码运行大约20秒并返回BUILD SUCCESSFUL (total time: 21 seconds)您对此问题的看法是什么?

I assume the issue is, port 80 is reserved for the web (HTTP) server, not for MySQL. 我假设问题是,端口80是为Web(HTTP)服务器保留的,而不是为MySQL保留的。 The default port for MySQL is 3306. MySQL的默认端口是3306。

The single quotes will fix your problem... 单引号将解决您的问题......

state.executeUpdate("INSERT INTO deneme (name) VALUES ('" + name + "')");

By using a PreparedStament you will not need to mess with single and double quote combinations. 通过使用PreparedStament,您不需要混淆单引号和双引号组合。

Note that the reason why a try statement is required, is in order to report the exception, since many things may go wrong when accessing a database report the exception message and it will help you find the problem. 请注意,需要try语句的原因是为了报告异常,因为访问数据库时可能会出现许多错误报告异常消息,它会帮助您找到问题。

Also, it is always useful to check if the JDBC driver exists before trying to connect. 此外,在尝试连接之前检查JDBC驱动程序是否存在总是有用的。 Insert another try statement, like this one: 插入另一个try语句,如下所示:

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        String msg = "The com.mysql.jdbc.Driver is missing\n"
                + "install and rerun the application";
        JOptionPane.showMessageDialog(this, msg, this.getTitle(), JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }

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

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