web blazonry web development scripts and tutorials Get best price on Unlocked Moto G Stylus
   PHP       Name Generators       Perl       CSS       Javascript       Java       MySql       How Tos       Resources   

How Tos Home

Free Linux Books
  Programming for Linux
  Sys & Network Admin
  On the Desktop

Articles
Tech Tips for Picking a Web Host

Database Tutorial (PHP & MySQL)

How to Instal Apache + PHP + MySQL

PHP vs. ASP vs. JSP

Introduction to Debugging

MySQL JDBC Drivers

Overview of Client Side

Margins Flush with Edges of Browser

Specifying Alternate Document to Print

Web App Security
  Who Submit That?
  Cookie Security
  Passwords
  Secure Web Development

Bookmark and Share





MySQL JDBC Drivers

This article covers JDBC drivers for MySQL with notes about installation, code for working with the database, and a troublehooting lesson I learned the hard way that will hopefully save you lots of time and headaches.

There several JDBC drivers for MySQL. Probably the most common one, and the one I use is, MM.MySQL JDBC Driver. The MM driver was written by Mark Matthews and released under the GNU LGPL license. Source code is available.

Installing the MySQL JDBC Drivers

Installing the MySQL JDBC drivers is straightforward. Download the JAR file . Place it in your classpath. Note: This means the specific jar file needs to be in the classpath, not just in a directory that is in your classpath. For more information see the MM documentation.


Java Code for Working With Database

Here is a simple example to select a row from a database and to display that information.

/*== database constants ==*/
string dbhost = "localhost";
string dbname = "directory";
string dbuser = "webuser";
string dbpass = "--password--";

string dbdriver = "org.gjt.mm.mysql.Driver";

/*== setup database driver and connect ==*/
Class.forName(DBDRIVER).newInstance();
string conurl = "jdbc:mysql://"+dbhost+"/"+dbname;
Connection db = DriverManager.getConnection(conurl,dbuser,dbpass);

/*== create sql query and execute ==*/
string sql = " SELECT firstname,lastname FROM users ";

Statement stmnt = db.createStatement();
ResultSet rs = stmnt.executeQuery(sql);
/*== display results ==*/
while (rs.next())
{
  out.println("Name: "+rs.getString("firstname")+ " " + rs.getString("lastname") +"\n");
}


Troubleshooting MySQL Permissions

One problem I ran into that gave me a gigantic headache was the following error message:
SQLException: Server configuration denies access to data source

I got this error only on my home machine and not on my work machine that had the same exact setup, including username and password. I did not have this problem connecting to MySQL or using PHP to connect to MySQL using the same username/password.

The problem was a DNS-type issue. My server at home reports itself as localhost.localdomain which was not in my MySQL permission table. Only localhost was.

The solution was to add (duplicate) the "webuser" entry in the "mysql.user" table with one host set to 'localhost' and the other to '%'

The easiest way I know of doing this is to dump the mysql table:

# mysqldump mysql >table.dump

Then edit the table.dump file, deleting almost everything but the user entries you want, change these as mentioned above. It is easier to edit by dumping since you don't have to type and count the number of Y/N columns that are needed if you inserted the user. (See MySQL Permissions Documentation - link below)

After the change don't forget to reload permissions using mysqladmin reload on the command-line or flush privileges at the mysql command-line client.


Related Links

 

 

Newest Pages
Test Google Ads Ver. 2
Free Linux Admin Books
Free Linux Books for Programmers
Free Books for Linux on the Desktop
Free PHP Books
Free JavaScript Books
Free Java Books - Advanced
Free Java Books - Basic
Free Perl Books
Free Python Books
Quote of the Day (PHP)
Debugging Part 2
How to Test Google Ads
Most Popular Pages
Baby Name Generator
U.S. Name Generator
Wu Name Generator
Popup Windows (JavaScript)
Upload and Resize an Image (PHP)
How To Install Apache + PHP + MySQL
Intro to Web Databases (PHP, MySQL)

Least Popular Pages
iNews Applet (Java)
Java Resources
Site Monitor (Perl)
PHP Resources
 
 

  privacy policy     ||     © 1997-2016. astonishinc.com   All Rights Reserved.