【发布时间】:2011-03-23 13:44:38
【问题描述】:
我已经安装了 MySQL(上次更新)。 我需要编写代码,它将创建并建立与 SQL DB 的连接 & 管理数据库(使用 SELECT、INSERT、CREATE)。
我做了一切,但是我无法创建连接。我还安装了 MySQL/J 连接器,我只是将 .zip 包提取到一个文件夹中,并在 Variables 中添加了文件夹路径。
谁能告诉我下一行中的 URL 是指 wat 吗?
Connection connection = DriverManager.getConnection(url, username, password);
我试过这个:
String url = "jdbc:odbc:sqlserver://localhost:3306/myfirstdb";
Connection con = DriverManager.getConnection(url, "root", "1234");
但它不起作用。我无法理解“URL”一词。 谁能解释一下,'url'和wat的含义应该通过Java连接到SQL服务器。
更新:
这是完整的代码。它仍然无法连接。
import java.sql.*;
public class TestDriver {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//This s wat actually i did for connection
System.out.println("Driver Loaded Succesfully");
}
catch (Exception e){
System.out.println("Unable to Load Driver!!!");
}
try {
Class.forName(com.mysql.jdbc.Driver"); // initialise the driver
String url ="jdbc:mysql://localhost:3306/myfirstdb";
Connection con = DriverManager.getConnection(url, "root", "1234");
System.out.println("connection Established");
}
catch(Exception e) {
System.out.println("Couldnt get connection");
}
}
}
你能告诉我 MySQL Connector/J 的用途是什么吗?
【问题讨论】:
-
@Zaki, @Tamil Vendhan has stated below 正在使用 MySql。我已将 mysql 标签添加到问题中。
标签: java mysql jdbc connection