tags:

views:

106

answers:

7

Is it need install anything? And if need , I want to get a full step to follow. And a simple connection coding. Thank you

+6  A: 

See this tutorial.

In short:

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost/dbName", "root", "secret");

You can also use DriverManager.registerDriver(..) instead of the Class.forName(..).

Of course you will need to download the mysql-connection-x.jar (the jdbc driver) and put it on your classpath.

Bozho
+1. I would just add "make sure MySQL's JDBC driver is in your classpath".
Pablo Santa Cruz
And you need to download the driver for your database, as stated in the tutorial. MySQL also has examples on their dev. site.
extraneon
thanks , added.
Bozho
Configuring MySQL Database must be need?
Eric
well, yes, you must create a database using `CREATE DATABASE` (see the manual for more details)
Bozho
+3  A: 

First you have to download MySql Driver, and add it into your java library or class path. Then try this,

 try
   {
    Class.forName("com.MySql.jdbc.Driver");
    String Url = "jdbc:MySql://localhost:3306/databaseName";
    connection=DriverManager.getConnection(Url);
    String query="Select * from TableName";
    resultSet=statement.executeQuery(query); 
    }catch(Exception ex){
   System.out.println("Connection Error");
  }
Venkats
The classname and URL in this example are invalid.
BalusC
You've only half-corrected the classname from SQLServerDriver to Driver. They are both however *still* invalid.
BalusC
Thanks a lot...
Venkats
A: 

At the first line the "pagkage" have underlined, please let me know to solve it?

package com.stardeveloper.example;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

Eric
Either remove the first line or put the source in a directory tree com/stardeveloper/example.
duffymo
A: 

Once you have installed the driver, check this link: EMBEDDING SQL IN JAVA for details on how to connect to SQL Server database from Java database applications as well as C#.NET database applications. It also describes how to pass embedded SQL queries, calling stored procedures, pass parameter etc.

Shahriar Nour Khondokar: Shahriarnk.com/

SNK111
He's using MySQL, not SQL Server. And he's not using it in embedded mode; it's running in server mode.
duffymo
A: 

I'd refer to this:

http://dev.mysql.com/doc/refman/5.0/en/connector-j.html

It'll have all the examples you need to connect to MySQL using Connector-J.

duffymo
A: 

Class.forName("com.MySql.jdbc.Driver");

The "Driver" is it using followinf step to make out?

•Click Control Panel in the Windows OS. •Click Administrative Tools •Click Data Sources (ODBC) •In the form that appears, select Add •In the next form select the driver from the list – for example SQL Server

Eric
A: 

You can try using spring datasource. http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/jdbc/datasource/package-summary.html

I have found it convenient. Also, if it's a large scale application consider connection pooling too.

Maddy