Java 如何使用JDBC连接到本地主机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4436146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to local host using JDBC?
提问by Chan
I installed MySql on my own machine. I created database, create table, ... using MySql CommandLine Client. When working on a project in school, I connected to school's database using this syntax:
我在自己的机器上安装了 MySql。我创建了数据库,创建表,...使用 MySql 命令行客户端。在学校开展项目时,我使用以下语法连接到学校的数据库:
public static Statement connect() {
try {
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
conn = DriverManager.getConnection( "1", "2", "3" );
stmt = conn.createStatement();
}
catch( Exception e ) {
System.out.println( "Connection Error: " + e );
}
return stmt;
}
In my local machine, I don't have to type in user name, all I did is just login with my password as root user:
在我的本地机器上,我不必输入用户名,我所做的只是以 root 用户身份使用我的密码登录:
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.53-community MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use chandb;
Database changed
mysql> show tables;
+------------------+
| Tables_in_chandb |
+------------------+
| another |
| cars |
| employees |
+------------------+
3 rows in set (0.03 sec)
mysql> select * from Another;
+----+-----------+----------+
| Id | GoldValue | Model |
+----+-----------+----------+
| 0 | 100 | Civic DX |
+----+-----------+----------+
1 row in set (0.00 sec)
mysql>
I would like to know how can I connect to my local machine's database? what should I put as parameters within method .getConnection
我想知道如何连接到本地机器的数据库?我应该在方法 .getConnection 中放置什么作为参数
conn = DriverManager.getConnection(
"1", // ?
"2", // ?
"3" ); // ?
Best regards,
Chan
最好的问候,
陈
采纳答案by Mohamed Saligh
Simple Connection:
简单连接:
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] argv) throws Exception {
String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName);
String serverName = "localhost";
String mydatabase = "mydatabase";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "username";
String password = "password";
Connection connection = DriverManager.getConnection(url, username, password);
}
}
回答by Robert
It looks like you left your username and password inthe source you posted.
您似乎在发布的来源中留下了用户名和密码。
I don't see why you can't just do
我不明白你为什么不能做
DriverManager.getConnection("jdbc:mysql://localhost/chandb", "user, "pass");