First Method:
1. sudo zypper install mysql-connector-java (or go to Software Management and search for jdbc, you will find mysql-connector-java package).
2. Once it's installed, you'll have the file /usr/share/java/mysql-connector-java.jar (/usr/share/java/mysql.jar), which is an indirect symlink to the actual jar file.
3. in Eclipse, do this as: Project -> Properties -> Java Build Path -> Libraries -> Add External JARs -> select /usr/share/java/mysql-connector-java.jar
Or, Second Method:
Download the MySQL JDBC connector from http://www.mysql.com/downloads/connector/j/5.1.html.
[Platform Independent (Architecture Independent), Compressed TAR Archive]
Extract the JDBC driver JAR file from the downloaded file; for example:
tar zxvf mysql-connector-java-5.1.39.tar.gz
Add the JDBC driver, renamed, to the relevant server; for example:
$ sudo cp mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar /usr/share/java/mysql-connector-java.jar
If the target directory does not yet exist on this host, you can create it before copying the .jar file; for example:
$ sudo mkdir -p /usr/share/java/
$ sudo cp mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar /usr/share/java/mysql-connector-java.jar
I did the first method.
******************************
Test if JDBC Connection is Suffessful:
import java.sql.*;
class JdbcTest{
public static void main(String[] args) throws Exception {
System.out.println("Connecting database...");
Connection connection = null;
String url = "jdbc:mysql://localhost:3306/cs_project";
String username = "root";
String password = "vijaya";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, username, password);
System.out.println("Connection Successful.");
// Success.
} catch (SQLException e) {
// Fail.
System.out.println("Connection Failed."+e);
} finally {
if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}
}
}
I cannot thank you enough for the blog.Thanks Again. Keep writing.
ReplyDeleteonline training in java
online training on java