Is it need install anything? And if need , I want to get a full step to follow. And a simple connection coding. Thank you
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.
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");
}
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;
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/
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.
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
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.