-1

i'm new in java. I'm trying to connect mysql DB in java

i've got this

mysql.java file:

package program;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class mysql {
        private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
        private static final String DATABASE_URL = "jdbc:mysql://mysql.cba.pl:3306/dbname";
        private static final String USERNAME = "login";
        private static final String PASSWORD = "pass";
        private static final String MAX_POOL = "250";

    private Connection connection;
    private Properties properties;

    private Properties getProperties() {
        if (properties == null) {
            properties = new Properties();
            properties.setProperty("user", USERNAME);
            properties.setProperty("password", PASSWORD);
            properties.setProperty("MaxPooledStatements", MAX_POOL);
        }
        return properties;
    }

    public Connection connect() {
        if (connection == null) {
            try {
                Class.forName(DATABASE_DRIVER);
                connection = DriverManager.getConnection(DATABASE_URL, getProperties());
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

    public void disconnect() {
        if (connection != null) {
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

main java file:

package program;

public class main {

    public static void main(String[] args){
        mysql db = new mysql();
        db.connect();
    }

}

and i'm getting following errors

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at komunikator.mysql.connect(mysql.java:31)
at komunikator.main.main(main.java:7)

I downloaded ojdbc6-11.2.0.2.0.jar and put in my project folder, after that i add library from jar file. What i'm doing wrong ? I'm really new in java so i hope somebody can help me as well :)

I'm using Eclipse Standard/SDK

Version: Kepler Release Build id: 20130614-0229

Jarzyn
  • 19
  • 8
  • Duplicate of http://stackoverflow.com/questions/1585811/classnotfoundexception-com-mysql-jdbc-driver - you might be able to find a resolution from the answers there! – Aneesh Ashutosh May 11 '17 at 18:43
  • 1
    Possible duplicate of [ClassNotFoundException com.mysql.jdbc.Driver](http://stackoverflow.com/questions/1585811/classnotfoundexception-com-mysql-jdbc-driver) – Antimony May 12 '17 at 01:28

3 Answers3

0

ou need to use com.mysql.jdbc_5.1.5 driver for mysql connection and paste jar file in to WEB-INF>Lib folder and use the path of jar file into property>buildpath and use external jar.

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
  • `WEB-INF`? Who said anything about a web application? – avojak May 11 '17 at 18:45
  • then who said it is not a web application or it is desktop application – Anshul Sharma May 11 '17 at 18:46
  • Nothing in the question indicates that it is. If you're going to give an answer that's specific to a web application, you might want to make sure that the OP is building a web application first. – avojak May 11 '17 at 18:48
0

it will be a normal desktop application

ok, i changed it on com.mysql.jdbc_5.1.5 driver, i paste this in project path with others .java files and added like u said in properties. Now i'm getting this:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2104)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at program.mysql.connect(mysql.java:32)
    at program.main.main(main.java:7)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
    ... 13 more

Is mysql.java file correct ?

Edit: i tried change this

private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";

on

"com.mysql.jdbc_5.1.5.Driver"

and i am gettin some less errors

java.lang.ClassNotFoundException: com.mysql.jdbc_5.1.5.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at program.mysql.connect(mysql.java:31)
at program.main.main(main.java:7)
Jarzyn
  • 19
  • 8
0

Ok, almost i've got this. One thing more. When im trying connect to on-line DB i got this:

SQLException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
SQLState: 08S01
VendorError: 0

on localhost DB (xamp) it's working fine
what is going on ?

Jarzyn
  • 19
  • 8