开发者

How to connect MySQL to Java program

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 m开发者_Python百科e 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";


Here's an extract from your code:

} catch (Exception e) {
    System.out.println("Couldnt get connection");
}

You should never suppress exceptions as long as you don't understand its cause. Replace it by at least:

} catch (Exception e) {
    System.out.println("Could not get connection");
    e.printStackTrace();
}

Or maybe

} catch (Exception e) {
    throw new RuntimeException("Could not get connection", e);
}

Either way, you should see the exception type, message and trace. In your code snippet the possible exceptions are ClassNotFoundException and SQLException. The first one would mean that the driver is not properly placed in the classpath. The second one would mean that connection cannot be obtained. The exception message and/or trace should tell in detail about the underlying root cause of the problem.

You should always observe exceptions. They tell something about the cause of the problem. You know, once a cause is understood, the solution is nothing more than obvious :)

See also:

  • Short MySQL/JDBC tutorial - Contains explanation about exception causes.

Further,

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

An URL is an Uniform Resource Locator. It's a common way to locate (identify) unique resources in computer systems and networks. The URL syntax for the MySQL database is explained in the documentation of the JDBC driver.

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

It's the JDBC driver. The JDBC API exist of almost only interfaces. The DB vendors should provide their own concrete JDBC API implementation, which is the JDBC driver. With a JDBC driver you'll be able to connect a specific database using JDBC API.


If its MS SQL Server,

String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName(driver);
String url = "jdbc:microsoft:sqlserver://host:1433/database";
Connection conn = DriverManager.getConnection(url, "username", "password");

For more info, see this to get started with Microsoft JDBC.

You can use any of the two JDBC drivers for MSSQL:

  • Microsoft SQL Server JDBC Driver 2.0
  • jTDS

For MS SQL Server driver 2.0, use

URL: jdbc:sqlserver://server:port; DatabaseName=dbname
Class name: com.microsoft.sqlserver.jdbc.SQLServerDriver

For MySql & Java, see this on SO.


You forgot a " at Class.forName(com.mysql.jdbc.Driver"); It should be

Class.forName("com.mysql.jdbc.Driver");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜