Avatar billede fredand Forsker
18. oktober 2002 - 10:00 Der er 3 kommentarer og
2 løsninger

Connect to a databse?

Hello!

I wonder if it is possible, and how, to develop a stand alone application in Java that uses a database like Access or MySQL that can be downloaded and run on a client without that the user needs to create a odbc or install the database application?

With ASP I can do like this with a Access-file below so the ASP-app direct uses the file.

Set myDatabaseConnection = Server.CreateObject("ADODB.Connection")
databasePath = "../database/school.mdb"
connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
connectionString = connectionString & server.mappath(databasePath) & ";"
myDatabaseConnection.open connectionString

So is it possible to do it the same in Java? I have only found these two ways with either jdbc:odbc or jdbc for MySql:

    public MySQLTester()
    {
        try
        {
            Class.forName("org.gjt.mm.mysql.Driver");

            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=pass");

            PreparedStatement preparedStatement = conn.prepareStatement("SELECT * FROM users");

            ResultSet resultSet = preparedStatement.executeQuery();

            while ( resultSet.next() )
            {
                System.out.println( resultSet.getString("id") );
                System.out.println( resultSet.getString("name") );
                System.out.println( resultSet.getString("password") );
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public MySQLTester(String in)
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            Connection conn = DriverManager.getConnection("jdbc:odbc:test", "root", "pass");

            PreparedStatement preparedStatement = conn.prepareStatement("SELECT * FROM users");

            ResultSet resultSet = preparedStatement.executeQuery();

            while ( resultSet.next() )
            {
                System.out.println( resultSet.getString("id") );
                System.out.println( resultSet.getString("name") );
                System.out.println( resultSet.getString("password") );
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

Best regards
Fredrik
Avatar billede soreno Praktikant
18. oktober 2002 - 10:28 #1
it is possible to to connect to a remote database, but for the database to run on a local machine it has to be installed - either an odbc connection or an dbms like mysql. without installing it you don't have access to sql.

you could reverse engineer the .mdb file format, but it's not a feasible solution.. - you still need a server to take care of the sql statements.. :-)
Avatar billede fredand Forsker
18. oktober 2002 - 10:47 #2
So what you say is that I can't connect to a Access file (*.mdb) without a jdbc:odbc or odbc, at localhost, with some hardcoded path or something to the file?

Fredrik
Avatar billede soreno Praktikant
18. oktober 2002 - 11:00 #3
correct.
i don't know anything about asp but my guess is that "ADODB.Connection" in combined with "Microsoft.Jet.OLEDB.4.0" is a (sql)server that comes installed with iis.
Avatar billede dsj Nybegynder
18. oktober 2002 - 12:53 #4
In java you use the .jar-file containing the mysql-driver i guess. This .jar-file you can just ship with the java application and no further adjustments are necessary. You can get direct contact with a remote mysql-database.

With MS Access you will have to make some configurations one the machine to run the java application. Please keep away from this; it's not a full blod database and has some disadvanteges, especially when using java. It's slow lige hell, mysql is alot faster used with java
Avatar billede fredand Forsker
11. november 2002 - 10:02 #5
Thanks for your time mates!

Best regards

Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester