- The Coded One
- Share this:
- Like this:
- Related
- 26 Responses
- Introduction
- Installation
- Set up MySQL default password
- Create SQL Databases and tables
- User creation and privileges
- Eclipse
- Setting up the user to use JDBC
- Testing in Java
- Installing JDBC Driver
- Installing the Postgres JDBC Driver
- Installing the MySQL JDBC Driver for MariaDB
- Installing the MySQL JDBC Driver
- Installing the Oracle JDBC Connector
The Coded One
Assuming that you already have MySQL installed, the next step is to install the connector driver. You can do this easily on the CLI by using the following command:
sudo apt-get install libmysql-java
The next step is to make sure that the classpath is set. You can have this set automatically by adding this command to you bashrc file.
If you want to set this for all users, you should modify the /etc/environment instead.
For those using Eclipse, you can also do this by going through the following steps:
- Select Project Properties > Java Build Path
- Select Libries tab
- Click Add External Jars
- Choose the jar file, in this case mysql-connector-java.java
Once you’re done, you can test the connection using the following snippet:
import java.sql.Connection; import java.sql.DriverManager; class JDBCTest < private static final String url = "jdbc:mysql://localhost"; private static final String user = "username"; private static final String password = "password"; public static void main(String args[]) < try < Connection con = DriverManager.getConnection(url, user, password); System.out.println("Success"); >catch (Exception e) < e.printStackTrace(); >> >
Share this:
Like this:
Related
26 Responses
Subscribe to comments with RSS.
could you show me the way to set this class path in steps:
export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar
Your code was giving me an SQL exception.
This is what worked for me.. import java.sql.Connection;
import java.sql.DriverManager; class JDBCTest < private static final String url = “jdbc:mysql://localhost”; private static final String user = “root”; private static final String password = “vision$23”; public static void main(String args[]) try Class.forName(“com.mysql.jdbc.Driver”);
Connection con = DriverManager.getConnection(url, user, password);
System.out.println(“Success”); > catch (Exception e) e.printStackTrace();
>
>
>
im also getting d following exception after trying the code : java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:931)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4031)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1296)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2338)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2371)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:378)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:200)
at JDBCTest.main(JDBCTest.java:14) i changed d username and password.. it didnt work!
Introduction
This document explains how to set up Java applications to communicate with the MySQL Database. This can be used either in Java development, or applications which use JDBC such as OpenOffice.Org. See Using MySQL, JDBC and OpenOffice for more information.
This document was written originally for 5.10, but has been updated for 6.06. Changes required for 5.10 are shown in italics
It has also been checked against 6.10 (Edgy Eft) and 7.04 (Feisty Fawn), which should be installed as for 6.06
Installation
Install mysql client, server and the jdbc connector, either via synaptic or by using the following
sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install libmysql-java
On 5.10 you may need select «No configuration» for postfix if it is installed.
Set up MySQL default password
Set up the password for the root user as ‘root’ or whatever you want. The last entry is the password
mysqladmin -u root password root
Check you can connect to mysql
then enter the password you just set and press return again. In this case it would be «root».
Create SQL Databases and tables
Create databases and tables or whatever, so you have some data to work with. You can disconnect and use mysql-query-browser for this if you wish. For the sample code below just enter
create database emotherearth;
User creation and privileges
Create a user with access to that table. Replace the items in square brackets by the database name, and the chosen user and password. Don’t type in the square brackets !
grant all privileges on [database].* to [user]@localhost identified by "[password]"; flush privileges;
Note: Ubuntu 5.10 requires localhost.localdomain
This is required because when you connect using «mysql -u root -p» it connects directly to the Sql Server. The «grant» line creates access via 127.0.0.1 (localhost). This is no longer an issue on 6.10.
This will work only for the user you have just «granted», not for root. Counter-intuitively, the ‘default’ server and localhost/127.0.0.1 aren’t the same thing
Eclipse
For those who are using Eclipse, you will likely to have a ‘Class Not Found’ exception. To fix this, go to: Project, click Properties, select Java Build Path, and choose the Libraries tab. Then select ‘Add External JARs’, and find ‘/usr/share/java/mysql-connector-java.jar’.
Setting up the user to use JDBC
Add these two lines to /home/[user]/.bashrc. I am running Java 5 which doesn’t require the current directory to be on the Classpath ; I *think* Java 2 does, but I’m not going back to check it In that case you may need to have $CLASSPATH. /user/share/java/mysql.jar
CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar export CLASSPATH
Alternatively, you can set it for all users, by editing /etc/environment (use sudo — sudo vi /etc/environment)
CLASSPATH=".:/usr/share/java/mysql.jar"
Log out and Log in again. (If you only edited /home/[user]/.bashrc you don’t need to log out/in, only execute in a terminal «$source .bashrc» in your home dir). Start up a terminal and type:
It should print out something like «:/usr/share/java/mysql.jar»
Testing in Java
It should now work. Here is some typical code (clearing up removed for simplicity)
import java.sql.*; import java.util.Properties; public class DBDemo // The JDBC Connector Class. private static final String dbClassName = "com.mysql.jdbc.Driver"; // Connection string. emotherearth is the database the program // is connecting to. You can include user and password after this // by adding (say) ?user=paulr&password=paulr. Not recommended! private static final String CONNECTION = "jdbc:mysql://127.0.0.1/emotherearth"; public static void main(String[] args) throws ClassNotFoundException,SQLException System.out.println(dbClassName); // Class.forName(xxx) loads the jdbc classes and // creates a drivermanager class factory Class.forName(dbClassName); // Properties for user and password. Here the user and password are both 'paulr' Properties p = new Properties(); p.put("user","paulr"); p.put("password","paulr"); // Now try to connect Connection c = DriverManager.getConnection(CONNECTION,p); System.out.println("It works !"); c.close(); > >
JDBCAndMySQL (последним исправлял пользователь 149 2010-04-20 13:28:19)
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details
Installing JDBC Driver
Download, extract, and copy the JDBC driver, renamed, to /usr/share/java/ . If the target directory does not yet exist, create it.
Installing the Postgres JDBC Driver
- Install the PostgreSQL JDBC driver. If you would like to use the PostgreSQL JDBC driver version shipped with the OS repositories, run the following command:
yum install postgresql-jdbc*
cp /usr/share/java/postgresql-jdbc.jar /usr/share/java/postgresql-connector-java.jar
ls /usr/share/java/postgresql-connector-java.jar
chmod 644 /usr/share/java/postgresql-connector-java.jar
Installing the MySQL JDBC Driver for MariaDB
The MariaDB JDBC driver is not supported. Follow the steps in this section to install and use the MySQL JDBC driver instead.
Install the JDBC driver on the Cloudera Manager Server host, as well as any other hosts running services that require database access.
If you already have the JDBC driver installed on the hosts that need it, you can skip this section. However, MySQL 5.6 requires a version 5.1.26 or higher.
Cloudera recommends that you consolidate all roles that require databases on a limited number of hosts, and install the driver on those hosts. Locating all such roles on the same hosts is recommended but not required. Make sure to install the JDBC driver on each host running roles that access the database.
Using the yum install command to install the MySQL driver package before installing a JDK installs OpenJDK, and then uses the Linux alternatives command to set the system JDK to be OpenJDK. If you intend to use an Oracle JDK, make sure that it is installed before installing the MySQL driver using yum install . If you want to use OpenJDK, you can install the driver using yum .
Alternatively, use the following procedure to manually install the driver.
- Download the MySQL JDBC driver from http://www.mysql.com/downloads/connector/j/5.1.html (in .tar.gz format). As of the time of writing, you can download version 5.1.46 using wget as follows:
wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.46.tar.gz
tar zxvf mysql-connector-java-5.1.46.tar.gz
sudo mkdir -p /usr/share/java/ cd mysql-connector-java-5.1.46 sudo cp mysql-connector-java-5.1.46-bin.jar /usr/share/java/mysql-connector-java.jar
sudo zypper install mysql-connector-java
sudo apt-get install libmysql-java
Installing the MySQL JDBC Driver
Install the JDBC driver on the Cloudera Manager Server host, as well as any other hosts running services that require database access.
If you already have the JDBC driver installed on the hosts that need it, you can skip this section. However, MySQL 5.6 requires a 5.1 driver version 5.1.26 or higher.
Cloudera recommends that you consolidate all roles that require databases on a limited number of hosts, and install the driver on those hosts. Locating all such roles on the same hosts is recommended but not required. Make sure to install the JDBC driver on each host running roles that access the database.
Using the yum install command to install the MySQL driver package before installing a JDK installs OpenJDK, and then uses the Linux alternatives command to set the system JDK to be OpenJDK. If you intend to use an Oracle JDK, make sure that it is installed before installing the MySQL driver using yum install . If you want to use OpenJDK, you can install the driver using yum .
Alternatively, use the following procedure to manually install the driver.
- Download the MySQL JDBC driver from http://www.mysql.com/downloads/connector/j/5.1.html (in .tar.gz format). As of the time of writing, you can download version 5.1.46 using wget as follows:
wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.46.tar.gz
tar zxvf mysql-connector-java-5.1.46.tar.gz
sudo mkdir -p /usr/share/java/ cd mysql-connector-java-5.1.46 sudo cp mysql-connector-java-5.1.46-bin.jar /usr/share/java/mysql-connector-java.jar
sudo zypper install mysql-connector-java
sudo apt-get install libmysql-java
Installing the Oracle JDBC Connector
You must install the JDBC connector on the Cloudera Manager Server host and any other hosts that use a database.
- Download the Oracle JDBC Driver from the Oracle website. For example, the version 6 JAR file is named ojdbc6.jar . For more information about supported Java versions, see Java Requirements. To download the JDBC driver, visit the Oracle JDBC and UCP Downloads page, and click on the link for your Oracle Database version. Download the ojdbc6.jar file (or ojdbc8.jar , for Oracle Database 12.2).
- Copy the Oracle JDBC JAR file to /usr/share/java/oracle-connector-java.jar . The Cloudera Manager databases and the Hive Mestastore database use this shared file. For example:
sudo mkdir -p /usr/share/java sudo cp /tmp/ojdbc8-12.2.0.1.jar /usr/share/java/oracle-connector-java.jar sudo chmod 644 /usr/share/java/oracle-connector-java.jar