-1

I have been looking forever how to connect to a database using java.

I Just used netbeans to create the data base, the url is

jdbc:derby://localhost:1527/project

However im using the terminal to compile and execute the java code instead of netbeans but I still have the error message that the no suitable driver found etc.. Here is the code. If anyone can help.

Thank you very much, it is really appreciated. I know that my question have been posted by I have tried everything and it dosnt work.

import java.sql.*;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Connection;

import java.sql.Statement;

import com.mysql.jdbc.Driver;



 public class Test {

        public static void main (String[] args){

        String host="jdbc:derby://localhost:1527/project";
        String name="groupe3";
        String password= "password";


        try{
        Class.forName("com.mysql.jdbc.Driver");

        Connection conn= DriverManager.getConnection(host,name,password);

        Statement insert=conn.createStatement();  
        insert.executeUpdate("INSERT INTO Login VALUES('apple',2)");
        insert.close(); 
        }

        catch(SQLException err){
            System.out.println(err.getMessage());}

        catch (ClassNotFoundException e) {
        e.printStackTrace(); }




}
}
Jeff
  • 12,555
  • 5
  • 33
  • 60
  • Your question says that you have "tried everything and it doesn't work". When asking questions on Stack Overflow, you'll get better results if you explain *specifically* what you tried (eg "I copied mysql_driver.jar to my app's `lib` folder") and *specifically* what happened ("The terminal printed the following error message: ...") – RJHunter Oct 28 '16 at 02:31
  • Hi Jeff, yes you are right.. In the begining it didnt work cause of the jar files but then I copied them in my library etc but if you read the title you will see the error msg which the terminal printed : no suitable driver ....... Thanks for your reply. – Missou Neili Oct 28 '16 at 03:42

1 Answers1

1

It looks like you've created a derby database, and are trying to use a mysql database driver.

https://db.apache.org/derby/docs/10.4/devguide/cdevdvlp40653.html

supamanda
  • 199
  • 2
  • 3
  • I never created a database before ... So if you can tell me how can I solve that – Missou Neili Oct 28 '16 at 00:16
  • Make friends with google, http://www.wikihow.com/Create-a-Database-in-MySQL – supamanda Oct 28 '16 at 00:18
  • I thought that I created the sql database with netbeans... anyways thanks for your assistance supamanda – Missou Neili Oct 28 '16 at 00:20
  • 1
    you may have created the database with netbeans, but there are different types of SQL databases. MySQL is one type, Derby is another, postgres is another. You need to make sure the jdbc driver you use matches your database. – supamanda Oct 28 '16 at 00:24
  • Thanks ! I solve my problem but I still have one more question, The program works perfectly on netbeans it connect to the database and everything. . I downloded the derbyclient jar file and I copied it in the lib of jdk and jre. When I run the code on the terminal it is still showing that there is no suitable driver etc .. I had the same problem with netbeans and I had to import the derby jar files . I thought I can do the same using the terminal ... Thanks for your help again !:) – Missou Neili Oct 28 '16 at 19:48
  • @MissouNeili If http://stackoverflow.com/a/20589785/39223 doesn't help, then open a new question. Explain what you've tried so far, including exact error messages. Remember, Stack Overflow is a Q&A site, not a discussion forum, so we try to keep things to a particular format. – RJHunter Oct 28 '16 at 22:04