简体   繁体   English

jdbc到mysql的连接

[英]connection between jdbc to mysql

I have the next code.我有下一个代码。 I save it in the file: 'MysqlConnect.java'.我将它保存在文件中:'MysqlConnect.java'。

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbctutorial";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root"; 
    String password = "root";
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url+dbName,userName,password);
        System.out.println("Connected to the database");
        conn.close();
        System.out.println("Disconnected from database");
    } catch (Exception e) {
        e.printStackTrace(); 
    }
  }
}

I know how to run it:我知道如何运行它:

javac MysqlConnect.java
java MysqlConnect

I have some questions:我有一些问题:

1) where should I save this file? 1)我应该在哪里保存这个文件? (I have a folder of: mysql-connector-java-5.1.22). (我有一个文件夹:mysql-connector-java-5.1.22)。

2) I created a database and a table in mysql. 2)我在mysql中创建了一个数据库和一个表。 I don't need to insert that into the code?我不需要将其插入代码中? for example:例如:

String dbname = "database_alon";

3) If the answer of the second question is 'YES', I only have a password to my database, not an username. 3)如果第二个问题的答案是“是”,我的数据库只有密码,没有用户名。 so what should I insert to the parameter of userName?那么我应该在userName的参数中插入什么?

so, I think that I have to change the code to:所以,我认为我必须将代码更改为:

String dbName = "database_alon";
String driver = "com.mysql.jdbc.Driver";
String userName = ""; 
String password = "ADMINALON";

right?对? and what about the question 1?那么问题1呢?

You can have your MysqlConnect.java file wherever you want.你可以在任何你想要的地方拥有你的 MysqlConnect.java 文件。 You should just add the folder containg the MysqlConnect.class in your classpath.您应该只在类路径中添加包含 MysqlConnect.class 的文件夹。 Moreover, in order to run properly you should also add the mysql-connector-java-5.1.22.jar to the classpath in order to be able to find the classes you require for running your class.此外,为了正常运行,您还应该将 mysql-connector-java-5.1.22.jar 添加到类路径中,以便能够找到运行类所需的类。

In order to avoid mutiple additions in the classpath you could add the mysql-connector-java-5.1.22.jar in the folder containing the MysqlConnect.class and make only one update of the classpath.为了避免在类路径中多次添加,您可以在包含 MysqlConnect.class 的文件夹中添加 mysql-connector-java-5.1.22.jar 并且只对类路径进行一次更新。

As for your second question YES you need to change the code and add the至于你的第二个问题你需要更改代码并添加

String dbname="database_alon"

instead of代替

String dbName = "jdbctutorial";

I dont think you dont have a username in you database because it picks the root username by default.我认为您的数据库中没有用户名,因为它默认选择根用户名。 Try root for username and tell me what it gives you but make sure to update your classpath as stated above.尝试使用root作为用户名并告诉我它为您提供了什么,但请确保如上所述更新您的类路径。 Make also sure you are using the default port 3306 for connecting to the database.还要确保您使用默认端口 3306连接到数据库。


PS What program you use for managing you databases? PS 你用什么程序来管理你的数据库? I can provide more help knowing that.知道这一点,我可以提供更多帮助。

PSS You could use an IDE such as Eclipse or Netbeans and that could make the deployment much more easier. PSS 您可以使用 Eclipse 或 Netbeans 等 IDE,这可以使部署更加容易。 You will not need to touch the classpaths and other variables because the IDE will take care by just selecting the jars you want to use for your project.您不需要接触类路径和其他变量,因为 IDE 将通过选择您要用于项目的 jar 来处理。

PSS If you dont know how to add a jar in your classpath please provide your operating system in order to provide help for that. PSS 如果您不知道如何在类路径中添加 jar,请提供您的操作系统以便为此提供帮助。

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

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