Skip to content

2020Media

 

Newsflash

Block your brand from becoming a .xxx Find out more.
 
You are here: Home arrow Tools arrow Dynamic Content inc Java arrow Java and MySQL
Java and MySQL PDF Print E-mail

Java & MySQL

The full documentation and information about MySQL can be found at http://www.mysql.com/documentation/index.html, Alternatively, MySQL by Paul DuBois is a good reference (ISBN 0-7357-0921-1)

Test your MySQL Servlet Account is working

At this point it is assumed that the basic servlets are working. To see how to test this go to Using Servlets

To test that the connection to MySQL is working, open up a web browser and go to

http:// webserver /mountpoint / testDB2020

where

webserver is the name for your web server
mountpoint is the name of the mount points for servlets.

Both of these are in the account detail letter.

This should bring up a page looking like:

2020Media Database Servlet Connection Working

You have connected successfully

If this does not happen, then contact support.

Connecting to MySQL

The following code fragments show the supported method of connecting to MySQL.



import java.sql.*;

import java.io.*;

import java.util.*;



import javax.servlet.*;

import javax.servlet.http.*;



......



public static final String DB_URL = "jdbc\:mySQL\://db_servername/db_database";

public static final String DB_USERNAME = "db_username";

public static final String DB_PASSWORD  = "db_password";

public static final String DB_DRIVER = "org.gjt.mm.mysql.Driver";



......



public void init() throws ServletException

{

try

{

Class.forName(DB_DRIVER);

}

catch(java.lang.ClassNotFoundException e)

{

......

}

try

{

con = DriverManager.getConnection(DB_URL,

DB_USERNAME, DB_PASSWORD);

}

catch(SQLException ex)

{

...........

}

where
db_username , db_password, db_servername and db_database are detailed in your account information.

We urge you to use code as close to this as possible, as it aids our debugging of any problems.

The source for the test page is available in the 'src' directory.

Accessing the Database

To access the database for creating tables and monitoring status, you may need to run an MySQL client. This can be done either from the command line or from your own computer.

Using the command line

There is a command line client which can be accessed by typing

mysql -u db_username -p -h db_hostname

This allows you to access the database from the command line. It is not very user friendly, but allows you to execute SQL commands directly.

You will then be prompted for your Database password. It should then return a prompt. You can then type in commands - followed by the word ';' .

Example

    show databases;



This should return a list of databases and some information about them. More details on using MySQL can be found here.

 
< Prev   Next >
Advertisement

Domain Search

Preferred Extensions -
.com .co.uk
.net .org
.me .eu
[+]
  • Narrow screen resolution
  • Wide screen resolution
  • Auto width resolution
  • Increase font size
  • Decrease font size
  • Default font size