jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Connect My Java Prgram to a mysql database. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Connect My Java Prgram to a mysql database." Watch "Connect My Java Prgram to a mysql database." New topic

Connect My Java Prgram to a mysql database.

Charles Sparking

Joined: Sep 09, 2012
Posts: 10
This will not allow me to connect to the mysql database which i have installed on my computer.In addition I have installed connector/j into the same directory. I have opened the class driver that resides in the org.gjt.mm.mysl folder in my IDE (Jcreator) and i have placed in the same directory with my ProductQuery class.
What I am actually trying to achieve is I have 2 input files that I require a java program to read and populate the tables. cam anyone help. see the question below

Please complete the Java exercise below.

Assumptions and prerequisites for this project:
- There exist a database with following two tables:
o ORDER [ ORDER_ID (Primary Column), ORDER_NO, PERSON_ID ]
- Input files to be processed:
o Person.data – The file has Person ID, First Name, Last Name, Street, City separated by comma (‘,’).

o Order.data – The file Order ID, Order Number and Person ID separated by pipe (‘I’) character. Person ID is present in both the files.

Requirement - Write a Java program to:
- Read the files (Person.data and Order.data) and populate the tables (PERSON and ORDER)
- Fetch data from these two database tables (using plain JDBC) and print:
o Persons with at least one Order
o All Orders with First Name of the corresponding person (if available)

Artefacts to be submitted at the end of this exercise:
- A zipped version of the source code with a project summary write up.

Mandatory requirements:
- Java 5 or above should be used.
- No third party libraries other than JDBC and JUnit
- The unzipped project files should be portable to a standard IDE such as Eclipse or NetBeans

The project work will be evaluated based on:
- Usage of Java coding standards
- Usage of control statements (if, for, while, switch, etc.), Collection API
- Utilisation of OOPs concepts
- Efficiency of the code
- Exception Handling
- Usage of Design Patterns
- Test Cases (preferably in JUnit)

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32621

Can you show a small code sample that illustrates the error and the error message?

Right now it looks like a homework assignment and as statement that "what I tried didn't work". You did write more than that, but not enough to provide a useful answer.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Charles Sparking

Joined: Sep 09, 2012
Posts: 10
Thanks for taking the time out to reply to me . I appreciate it. I have posted some code below.

import java.io.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ProductQuery extends JFrame
//the attributes
public static final String driver="C:Users.Public.DT Ikemefuna 2 MAR 13.Desktop.PCW Data Backup 10.09.10.Documents and Settings.CHARLES.JCreatorV3LE.MyProjects.Hello.system.RandomFiles.mysql-connector-java-5.0.8.src.org.gjt.mm.mysql.Driver";
public static final String url

public static final String username="U1098765";
public static final String password="scott";

private Connection con;
private Statement st;
private ResultSet result;
private JTextArea display=new JTextArea(25,26);

public ProductQuery()
//configure the frame and text area

setTitle("Product query");
display.setFont(new Font("DialogueInput",Font.BOLD,14));

//load the Mysql jdbc driver
catch(ClassNotFoundException e)
display.setText("Driver not found");

//connect to the database
//create an SQL statement
//execute an SQL query
result=st.executeQuery("select *from workers");

//create a handling

//display results
while(result.next())//move to next record
//retrieve and display first field
//retrieve the second record
//retrieve the third record
//retrieve the fourth record
catch(SQLException e)//handle the SQL exception



So I am trying to get the information displayed in the GUI. But all I get is a "Driver not found". So it compiles and displays this in the GUI display area. This would be a ClassNotFoundException as the code suggests but I dont understand because when I try to compile the Driver class file.This is the one off the connector/j installation
package com.mysql.jdbc does not exist

package org.gjt.mm.mysql;

import java.sql.SQLException;

* Here for backwards compatibility with MM.MySQL
* @author Mark Matthews
public class Driver extends com.mysql.jdbc.Driver {
// ~ Constructors
// -----------------------------------------------------------

* Creates a new instance of Driver
* @throws SQLException
* if a database error occurs.
public Driver() throws SQLException {
I agree. Here's the link: http://aspose.com/file-tools
subject: Connect My Java Prgram to a mysql database.
jQuery in Action, 3rd edition