All,

After installing MYSQL, APACHE, PHP.. My first goal was to get PHP working with MYSQL.

That is pretty easy (Just requires editing the php.ini file) and now you have the Web 
Based

PHP communicating with your MYSQL database. So at least you know something is working

and in fact, I got the proper arguments for my JAVA program to connect to MYSQL in a 
PHP Script.

Perhaps my oversite but as a Re-training JAVA programmer, with my Simple Goal to 
Connect

 and extract one field out of the MYSQL database, (knowing that would definately

validate the Jconnect Driver as well) I went searching for a "Simple" test JAVA 
program.

Thanks to the JAVA database programming Bible by John Donahue, There was a simple 
example

(Although it's not on the wiley.com [source code] web page.. It's easily typed in.)

and some borrowed lines form other programs and the paramaters from the PHP scripts.

Bottom Line.. This is a working, simple example to test the Jconnector to MYSQL.

NOTE: You have to do what they say and move the JAR File to the EXT library

My database in this example is named MYMATCH and the table name is uonline.

So here it is.. It  works.. I hope they post this or clean it up and put it into the

distribution of the JCONNECTOR..  SIMPLE.  In fact, if someone wants  to clean

it up and put the standard "TEST" [Standard meaning its a database already distributed

with MYSQL" into this example.. We all would connect and test or JAVA/MYSQL right away!

Best Regards

David




//package java_databases.ch04;

import java.sql.*;

import javax.sql.*;

import java.awt.*;

import javax.swing.*;



public class jdbcdemo{

public static void main(String args[]){

String dbtime;

String dbUrl = "jdbc:mysql:///mymatch";

String dbClass = "com.mysql.jdbc.Driver";

String query = "Select onlinetime FROM uonline";

try {

String newDbUrl = System.getProperty("com.mysql.jdbc.mymatch.url");

if ((newDbUrl != null) && (newDbUrl.trim().length() != 0)) {

dbUrl = newDbUrl;

}

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

Connection con = DriverManager.getConnection (dbUrl);


Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {

dbtime = rs.getString("onlinetime");

System.out.println(dbtime);

}

con.close();

}

catch(ClassNotFoundException e) {

e.printStackTrace();

}

catch(SQLException e) {

e.printStackTrace();

}

}

}
















//package java_databases.ch04;

import java.sql.*;

import javax.sql.*;

import java.awt.*;

import javax.swing.*;



public class jdbcdemo{

public static void main(String args[]){

String dbtime;

String dbUrl = "jdbc:mysql:///mymatch";

String dbClass = "com.mysql.jdbc.Driver";

String query = "Select onlinetime FROM uonline";



// Display the four images in row order in a 2 x 2 grid.

setLayout(new GridLayout(2, 2));

// Add the components, starting with the first entry in the

// first row, the second, etc.

add(new ScrollingImagePanel(david.jpg, 10, 10));

// add(new ScrollingImagePanel(im2, width, height));

// add(new ScrollingImagePanel(im3, width, height));

// add(new ScrollingImagePanel(im4, width, height));

pack();

show();





try {

String newDbUrl = System.getProperty("com.mysql.jdbc.mymatch.url");

if ((newDbUrl != null) && (newDbUrl.trim().length() != 0)) {

dbUrl = newDbUrl;

}

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

Connection con = DriverManager.getConnection (dbUrl);


Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {

dbtime = rs.getString("onlinetime");

System.out.println(dbtime);

}

con.close();

}

catch(ClassNotFoundException e) {

e.printStackTrace();

}

catch(SQLException e) {

e.printStackTrace();

}

}

}






Reply via email to