How connect to MySQL database using JDBC MySQL connector?
Download MySQL connector
Add connector to your Java project. It is simple - just click right mouse button on project and choose "Build Path" --> "Configure Build Path" --> "Java Build Path" and add connector *.jar library.
1. Here is simple, short example how to register and connect do MySQL via JDBC connector:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBexample
{
public static void main(String[] args) throws SQLException
{
Connection conn = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
try
{
conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "password");
}
catch (SQLException e)
{
System.out.println("Check DB link / user / password");
e.printStackTrace();
}
}
catch(ClassNotFoundException e)
{
System.out.println("There is no connector");
System.out.println(e.getMessage());
System.exit(-1);
}
System.out.println("Connector registered");
conn.close();
}
}
That's all!
No comments:
Post a Comment