Hi All,

I am new to use of database in Java. I am trying to write a simple java application which connects to my Database and sends some query and retrieves the output.

I have downloaded MariaDB and then created a database named iff and created tablespaces which has some information.

I have also downloaded JDBC driver from MariaDB site "mariadb-java-client-1.1.3" and also imported this JAR file in my project library in Eclipse.

Im facing hardtime connecting to my database. I have tried some Syntaxes available in Websites but nothing worked.

Below is my program which DID NOT connect ,
Please help me how to connect to database and run some basic queries.

FYI
Database Name iff
Location of Database Installed (C:\Program Files (x86)\Common Files\MariaDBShared\HeidiSQL)
JDBC driver Name mariadb-java-client-1.1.3 (Jar File)
Location of JDBC D:\JDBC

import java.sql.Connection;
import java.sql.DriverManager;

import javax.sql.*;

public class UserService {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String url = "jdbc:mysql://localhost:3306/"; 
        String dbName = "iff"; 
        String driver = "com.mysql.jdbc.Driver"; 
        String userName = "root";  
        String password = "root"; 
        try { 
        Class.forName(driver).newInstance(); 
        Connection conn = DriverManager.getConnection(url+dbName,userName,password); 

        conn.close(); 
        } catch (Exception e) { 
        e.printStackTrace(); 
        } 
        } 


    }

Below is my program which DID NOT connect ,

do you see why "which DID NOT connect" doesn't help us? are you getting an error message?
this would be the best place for you to start, but have you considered the possibility that you're DB is not running?

Hi Stultuske,

Below are the error message im getting in the console window

java.lang.ClassNotFoundException: driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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 com.tcs.iff.service.UserService.main(UserService.java:26)

And i checked my database by opening MariaDB, it works fine. Please let me know if additional info needed.

looks like there's something wrong with locating your driver, and this kind of baffles me too:

you say that you use: 'MariaDB' (can't say I know it), yet you seem to use: '"com.mysql.jdbc.Driver"; ' as Driver...

that's a Driver for MySQL, not for MariaDB. why don't you use MySQL? it's free, and offers all the functionalities that you'll need/want. not to mention it's very well supported.

I just heard that mySql is not free anymore and they suggested me to use MariaDB as it is developed by mysql team.

I downloaded JDBC named " mariadb-java-client-1.1.3 " (JAR file)

is this name i need to put for String driver in my code ?

Let me also tell what i did ..

I downloaded the above and kept in in D drive and added the Jar file to my program in Eclipse and ran.

Did i miss any steps here ? do i have to do something like installing JDBC or somethin like that...

Thanks Stultuske , James

I have now replaced JDBC driver from Maria DB client to MySql connector and it is connecting to my database now.

I hava also gone to the webstite which you have posted to download mySql open source database and downloaded the zip file for Win32. after unzipping it there are ten folders inside it.

1.bin 2.data. 3.docs 4.include 5.lib 6.mysql-test. 7.scripts 8.Share 9.sqlbench 10.supportfiles.

I could not find the installer anywher to install the database. Then how can i run the database ?

you don't need to install it, you just need to run it. no doubt you'll find sufficient documentation on the mysql page for that.

Thanks Stultuske

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.