aspose file tools*
The moose likes JDBC 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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connect My Java Prgram to a mysql database." Watch "Connect My Java Prgram to a mysql database." New topic
Author

Connect My Java Prgram to a mysql database.

Charles Sparking
Greenhorn

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 PERSON [ PERSON_ID (Primary Column), LAST_NAME, FIRST_NAME, STREET, CITY ]
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
Marshal

Joined: May 26, 2003
Posts: 30752
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Charles Sparking
Greenhorn

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
="jdbc:mysql://localhost/3306";

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");
add("Center",display);
setSize(520,250);
setVisible(true);
display.setTabSize(16);
display.setFont(new Font("DialogueInput",Font.BOLD,14));

try
{
//load the Mysql jdbc driver
Class.forName(driver);
}
catch(ClassNotFoundException e)
{
display.setText("Driver not found");

}
try
{
//connect to the database
con=DriverManager.getConnection(url,username,password);
//create an SQL statement
st=con.createStatement();
//execute an SQL query
result=st.executeQuery("select *from workers");

//create a handling
display.setText("Serial#"+"\t"+"Make"+"\t"+"Description"+"\t"+"Price"+"\n");
display.append("_____"+"\t"+"____"+"\t"+"_____"+"\t"+"___"+"\n");


//display results
while(result.next())//move to next record
{
//retrieve and display first field
display.append(result.getString(1)+"\t");
//retrieve the second record
display.append(result.getString(2)+"\t");
//retrieve the third record
display.append(result.getString(3)+"\t");
//retrieve the fourth record
display.append(result.getString(4)+"\t");
}
}
catch(SQLException e)//handle the SQL exception
{
e.printStackTrace();

}
}




}


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 {
super();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connect My Java Prgram to a mysql database.