database connection

how can i connnect mysql or sqlserver or msaccessdatabase with a java program.PLEASE give me sample code.
[112 byte] By [chamila1986a] at [2007-11-27 4:58:02]
# 1
http://java.sun.com/docs/books/tutorial/jdbc/index.htmlAnd specific for MySQL: http://dev.mysql.com/doc/refman/5.0/en/connector-j-examples.htmlAnd you can find similar documentation for the others.I.E. Google
masijade.a at 2007-7-12 10:13:48 > top of Java-index,Java Essentials,New To Java...
# 2

the three database engines differ in the connection way:

1 - due to mysql you need to do the following:

a - downloda the following file:

http://mysql.easynet.be/Downloads/Connector-J/mysql-connector-java-3.1.14.tar.gz

and extract it. you will find a jar file inside it (mysql jdbc driver) called mysql-connector-java-x.x.x-bin.jar

copy the last jar file into your class path.

b - copy and compile the following code:

import java.sql.Connection;

import java.sql.DriverManager;

public class ConnectionTest

{

public static void main(String args[])

{

connect();

}

public static void connect()

{

try

{

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

String url = "jdbc:mysql://<database-host-name>:<database-port>/<database-name>";

String userName = "<your-database-schema-user-name>";

String password = "<your-database-schema-password>";

Connection con = DriverManager.getConnection(url, userName, password);

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

after compiling this code, you then have established a connection to mysql database.

2 - Due to microsoft access you can connect through the JDBC/ODBC Bridge without downloading any driver.

3 - Due to sql server, I think you have to install specific software ( called Microsoft SQL Server 2000 driver for JDBC) to can connect.

for further reading this is jdbc tutorial page : http://java.sun.com/docs/books/tutorial/jdbc/index.html

Good Luck

Ahmad Elsafty

NourElsaftya at 2007-7-12 10:13:48 > top of Java-index,Java Essentials,New To Java...