Get best price on Unlocked Moto G Stylus | ||||||||||||
|
||||||||||||
|
MySQL JDBC DriversThis 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 DriversInstalling 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 DatabaseHere 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 PermissionsOne problem I ran into that gave me a gigantic headache was the following error message: 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
|
privacy policy || © 1997-2016. astonishinc.com All Rights Reserved. |