如何连接MySQL到Java程序 [英] How to connect MySQL to Java program

查看:161
本文介绍了如何连接MySQL到Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了MySQL(上次更新)。
我需要代码,创建&建立与SQL DB的连接
&管理数据库(使用SELECT,INSERT,CREATE)。



我做了一切,但我无法创建连接。我还安装了MySQL / J连接器,我只是提取了 .zip 包放在一个文件夹&

任何人都可以告诉我wat是通过下面一行的URL来表示的。

 连接connection = DriverManager.getConnection(url,username,password); 

我试过这个:

  String url =jdbc:odbc:sqlserver:// localhost:3306 / myfirstdb; 
Connection con = DriverManager.getConnection(url,root,1234);

但它不工作。我无法理解网址一词。
任何人都可以解释,'url'和wat的意思应该从Java连接到SQL服务器。






更新:



这是完整代码。它仍然无法连接。

  import java.sql。*; 

public class TestDriver {

public static void main(String [] args){
try {
Class.forName(sun.jdbc。 odbc.JdbcOdbcDriver); //这实际上是我的连接
System.out.println(Driver Loaded Succesfully);
}
catch(Exception e){
System.out.println(Unable to Load Driver !!!);
}

try {
Class.forName(com.mysql.jdbc.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);
}
}
}
<你可以告诉我wat是MySQL Connector / J的目的吗?

h2_lin>解决方案

在这个问题中,你似乎使用一个带有SQL Server jdbc URL的MySQL jdbc驱动程序,这将不起作用。



如果您使用 MySQL 数据库:

  Class.forName(com.mysql.jdbc.Driver); /初始化驱动程序

String url =jdbc:mysql:// localhost:3306 / myfirstdb;

如果使用SQL Server数据库,则需要一个完全不同的jdbc驱动程序。 jTDS 是开源的,是一个很好的选择。在类路径中包含jtds.jar文件,并使用类似的:

  Class.forName(net.sourceforge.jtds.jdbc .Driver); //初始化驱动程序

String url =jdbc:jtds:sqlserver:// localhost:1433 / myfirstdb;


I ve installed MySQL (last update). I need to code, that ll create & establish a connection with SQL DB & manage the DB(using SELECT, INSERT, CREATE).

I did everything but, I am not able to create connection. I've also installed the MySQL/J connector, I just extracted the .zip pack in a folder & added the folder path in Variables).

Can anyone tell me wat is meant by URL in the below line?

Connection connection = DriverManager.getConnection(url, username, password);

I ve tried this:

String url = "jdbc:odbc:sqlserver://localhost:3306/myfirstdb";
Connection con = DriverManager.getConnection(url, "root", "1234");

But it's not working. I am unable able to understand the term 'URL'. Can anyone explain, the meaning of 'url' and wat should be done to connect to a SQL server from Java.


Update:

This is the Full code. It still cannot connect.

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");
    }
    }
}

Can you tell me wat is the purpose of MySQL Connector/J?

解决方案

In the question you seem to be using a MySQL jdbc driver with a SQL Server jdbc URL. This won't work.

If you are using a MySQL database:

Class.forName("com.mysql.jdbc.Driver");  // initialise the driver

String url ="jdbc:mysql://localhost:3306/myfirstdb";

If you are using a SQL Server database you are going to need a completely different jdbc driver. jTDS is open source and a good option. Include the jtds.jar file in your classpath and use something like:

Class.forName("net.sourceforge.jtds.jdbc.Driver");  // initialise the driver

String url = "jdbc:jtds:sqlserver://localhost:1433/myfirstdb";

这篇关于如何连接MySQL到Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆