public static Connection getConnection(){
        if(connection==null){
            try{
                Class.forName("org.gjt.mm.mysql.Driver");
                connection=DriverManager.getConnection("jdbc:mysql://localhost:8084","root","ceyesuma");
            }catch(Exception exc){ connection=null;}
        }
        
        return connection;
    }

Here is the bean to get connected. Should this work?

package db;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;
/**
 *
 * @author James
 */
public class DBConnection {
    static String dbdriver;
    static String connURL;
    static String dbusername="";
    static String dbpassword="";
    static Connection connection=null;
    /** Creates a new instance of DBConnection */
    public DBConnection() {
    }
    public String getDbdriver(){
        return dbdriver;
    }
    public String getConnURL(){
        return connURL;
    }
    public String getDbusername(){
        return dbusername;
    }
    public String getDbpassword(){
        return dbpassword;
    }
    public void setDbdriver(String dbdriver){
        DBConnection.dbdriver=dbdriver;
    }
    public void setConnURL(String url){
        DBConnection.connURL=url;
    }
    public void setDbusername(String dbusername){
        DBConnection.dbusername=dbusername;
    }
    public void setDbpassword(String dbpassword){
        DBConnection.dbpassword=dbpassword;
    }
    public static Connection getConnection(){
        if(connection==null){
            try{
                Class.forName("org.gjt.mm.mysql.Driver");
                connection=DriverManager.getConnection("jdbc:mysql://localhost:8084","root","ceyesuma");
            }catch(Exception exc){ connection=null;}
        }
        
        return connection;
    }
    
}
package creditcard;
import db.DBConnection;
import java.lang.Object.*;
import java.io.*;
import java.sql.*;

import java.util.Date;
import tio.*;
import java.io.File.*;
import java.io.IOException;


class CreditCard {
    private long AccountNo;
    private double CreditLimit;
    public double TotalCharges;
    public double TotalPayments;
    private double Amount;
    private int TransResult;
    private String TransErr;
    private String Description;
    private String log=null;
    private String date;
    private FormattedWriter out;
    
      private Statement stm = null;
    private ResultSet rst = null;
    private String tableName = "acctdata";
    private String x;
    private String query;
    private DBConnection db;

    private Connection Conn;
    
    public CreditCard( ) throws FileNotFoundException, IOException  {
        newAccount();
        CreditLimit = 1000;
        TotalCharges = 0;
        TotalPayments =0;
   
    }
    
    public CreditCard(long AccountNo) throws FileNotFoundException, IOException {
        this.AccountNo = AccountNo;
       CreditLimit = 1000;
        TotalCharges = 0;
        TotalPayments =0;
        //validate AccountNO
        
    }
    private void accountStatus() throws IOException{
        
         String f = "CC_Num_status.txt";
        File file = new File(f);
        FormattedWriter fw=new FormattedWriter(f);
        if(f!=null){
               
                fw.printfln("Account: " + accountNumber());
            fw.printfln("Credit Limit: " + creditLimit());
            fw.printfln("Available Credit: " + Available());
            fw.printfln("Outstanding Balance: " + Balance());
            fw.printfln("Charge: " + Amount);
            fw.printfln("Description; " + Description);
            fw.printfln("payment: " + Amount);
            fw.printfln("Total Charges: " + TotalCharges);
            fw.printfln("Total Payments " + TotalPayments);
            //fw.printfln("\n");
           // fw.printfln("Transaction (0=Quit, +$=charge, -$=payment, 9999=Limit increase): ");
                
            }
    }
    private void newAccount() throws FileNotFoundException, IOException {
        double r = Math.random();
        AccountNo = (long)(r * 100000000);
       
    }
    
    public long accountNumber()  {
        return AccountNo;
    }
   
       
   
     private void writeLog(double Amount, String Description) throws FileNotFoundException, IOException, SQLException {
           this.AccountNo = AccountNo;
           Date dt = new Date();
           dt.getTime();
        String f = "CC" + AccountNo + ".txt";
       File log = new File(f);
       FormattedWriter out = new FormattedWriter(new FileWriter(log,true));
       out.printfln(AccountNo);
       out.printfln(dt.toString());
       out.printfln(Amount);
       out.printfln(Description);
       
            out.close();
            
            setData();
            
    }
     
     
   
    public String getTableName(){
        return tableName;
    }
    public void setTableName(String tableName){
        this.tableName = tableName;
    }
    
    public String getData() throws SQLException{
        if(tableName==null || tableName.equals("")){
            return "";
        }
       
        return x;
    }
    
     public void setData() throws SQLException{
        if(tableName==null || tableName.equals("")){
            return;
        }
        Connection conn =null;
        db.getConnection();
            String query = "INSERT INTO"+tableName+" VALUES(11111112,'2000-7-3',50,'THIS',25";
            Statement stm= conn.createStatement();
            stm.executeUpdate(query);
     }
    public double creditLimit()  {
        return CreditLimit;
    }
    
    public void creditIncrease()throws FileNotFoundException, IOException{
        TransResult = 0;
        if (Math.random() > .25) {
            CreditLimit += 100;
             accountStatus();
          
        } else { 
            System.out.println("Sorry, credit increase not possible at this time.");
            TransResult = 1;
        }
    }
    public double Available() {
        double Available = CreditLimit - ( TotalCharges - TotalPayments );
        return Available;
    }
    
    public double Balance() {
        double Balance = ( TotalCharges - TotalPayments );
        return Balance;
    }
    public void Transaction( double Amount, String Description ) throws FileNotFoundException, IOException, SQLException {
       
        TransResult = 0;
        
        if ( Amount == 0 ) {
            TransResult = 1;
            TransErr = "Transaction amount is 0.";
            return;
        }
        
        
        if ( Amount > Available() ) {
            TransResult = 1;
            TransErr = "Transaction amount of $" + Amount + " has exceeded the available credit limit $" + 				Available();
            return;
        }
        
        
        if ( Description == "" ) {
            TransResult = 1;
            TransErr = "No transaction description entered.";
            return;
        }
        if ( Amount > 0 ) {
            
            TotalCharges += Amount;
            accountStatus();
            writeLog(Amount, Description);
           
            
        } else if ( Amount < 0 ) {
            
            TotalPayments += -(Amount);
            accountStatus();
             writeLog(Amount, Description);
           
        }
    }

    

    
}
/*
* DBConnection.java
*
* Created on September 11, 2007, 11:50 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package db;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;
/**
*
* @author James
*/
public class DBConnection {
static String dbdriver;
static String connURL;
static String dbusername="";
static String dbpassword="";
static Connection connection=null;
/** Creates a new instance of DBConnection */
public DBConnection() {
}
public String getDbdriver(){
return dbdriver;
}
public String getConnURL(){
return connURL;
}
public String getDbusername(){
return dbusername;
}
public String getDbpassword(){
return dbpassword;
}
public void setDbdriver(String dbdriver){
DBConnection.dbdriver=dbdriver;
}
public void setConnURL(String url){
DBConnection.connURL=url;
}
public void setDbusername(String dbusername){
DBConnection.dbusername=dbusername;
}
public void setDbpassword(String dbpassword){
DBConnection.dbpassword=dbpassword;
}
public static Connection getConnection(){
if(connection==null){
try{
Class.forName(dbdriver).newInstance();
Class.forName("org.gjt.mm.mysql.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:8080","root","ceyesuma");
}catch(Exception exc){ connection=null;}
}

return connection;
}

}
DROP DATABASE IF EXISTS ccdb;
CREATE DATABASE ccdb;
USE ccdb;

CREATE TABLE acctData
( AccountNo 	        long Primary Key,
 transdate 		char(20),
 chrg	                double,
 transdesc	        VARCHAR(100),
 pymnt	                double) );

INSERT INTO acctData VALUES (99999999, '2000-7-3',30,'food',50);

Here is the rest of the program. This class test CreditCard

package creditcard;
//Credit Card Test - tests CreditCard class


import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import tio.*;

class CreditCardTest {
     
    public static void main(String[] args) throws IOException {
        new CreditCardTest();
    }
    public CreditCardTest() throws IOException{
        int inval;
        long AccountNo;
        double tranval=-1;
        String transdesc = null;
        CreditCard cc;        
        String f = "CC_Num_status.txt";
        File file = new File(f);
        FormattedWriter fw=new FormattedWriter(f);
        System.out.println("Welcome to the Credit Card simulator!");
        System.out.println("Existing Account, New Account, or Exit? (Existing Account=1, New Account=2, Exit=0): ");
        inval = Console.in.readInt();
        if (inval == 0)
            return;
        
        if (inval == 1) {
            System.out.println("Existing Account; enter Account #: ");
            
            AccountNo = Console.in.readLong();
            if (AccountNo == 0)
                return;
            
            cc = new CreditCard(AccountNo);
            
        } else {
            cc = new CreditCard();
        }
        
        while (tranval != 0) {
            System.out.println("Account: " + cc.accountNumber());
            System.out.println("Credit Limit: " + cc.creditLimit());
            System.out.println("Available Credit: " + cc.Available());
            System.out.println("Outstanding Balance: " + cc.Balance());
            System.out.println("Charge: " + + tranval);
            System.out.println("Description; " + transdesc);
            System.out.println("payment: " + tranval);
            System.out.println("Total Charges: " + cc.TotalCharges);
            System.out.println("Total Payments " + cc.TotalPayments);
            System.out.println("\n");
            System.out.println("Transaction (0=Quit, +$=charge, -$=payment, 9999=Limit increase): ");
            tranval = Console.in.readDouble();
            
            
            if (tranval == 0)
                break;
            
            if (tranval == 9999)
                cc.creditIncrease();
            else {
                if (tranval > 0) {
                    System.out.println("Transaction description: ");
                    transdesc = Console.in.readLine();
                    transdesc = Console.in.readLine();
					
                } else
                    transdesc = "payment";

				cc.Transaction(tranval, transdesc);
                
            }
        }
    }
}
Your code doesn't seem to be correct. It should be something thing like:

package db;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;
/**
 *
 * @author James
 */
public class DBConnection {
    private String connURL="jdbc:mysql://localhost:8084";
    private String dbusername="root";
    private String dbpassword="ceyesuma";
    private String dbdriver= "org.gjt.mm.mysql.Driver";
    private String dbname = "ccdb";
    private static DBConnection connection=null;
    /** Creates a new instance of DBConnection */
    private DBConnection() {
    }
    public String getDbdriver(){
        return dbdriver;
    }
    public String getConnURL(){
        return connURL;
    }
    public String getDbusername(){
        return dbusername;
    }
    public String getDbpassword(){
        return dbpassword;
    }
    public void setDbdriver(String dbdriver){
        this.dbdriver=dbdriver;
    }
    public void setConnURL(String url){
        this.connURL=url;
    }
    public void setDbusername(String dbusername){
        this.dbusername=dbusername;
    }
    public void setDbpassword(String dbpassword){
        this.dbpassword=dbpassword;
    }
    public void setDbname(String dbname){
        this.dbname = dbname;
    }
    public String getDbname(){
       return dbname;
    }
    public static DBConnection getInstance(){
        if(connection==null){
           connection = new DBConnection();
        }
        return connection;
    }
    public Connection getConnection() throws Exception{
       Connection connection = null;
       Class.forName(driver);
       connection = DriverManager.getConnection(connURL +"/" + dbname, dbusername, dbpassword);
       return connection;
    }
}

Moreover, if you are creating a bean, why are you hardcoding the connection related params? Why don't you use the bean setters to initialize the values??


I posted another version of my program. I put everything except getting the connection in it. about creating bean : eventually I would like to reuse it (I think) right now I am not sure. Thanks though. Could you look at the connection new

private String dbdriver= "org.gjt.mm.mysql.Driver";

this code is not reusable but for now it does make sense to be except the line above. i found that in an example and I am not sure it works for me I am using mysql

Connection id:          2
Current database:       ccdb
Current user:           root@localhost
SSL:                    Not in use
Using delimiter:        ;
Server version:         5.0.41-community-nt MySQL Community Edition (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
TCP port:               3306
Uptime:                 7 hours 33 min 56 sec

Threads: 1  Questions: 17  Slow queries: 0  Opens: 15  Flush tables: 1  Open tab
les: 0  Queries per second avg: 0.001
--------------

mysql>

Thanks
GO RAMS!

Hi,

I have made some changes to the source code and made it work. You have to do the additional CreditCard related stuff yourself. In any case feel free to drop a message. Find the source in src.zip attached with this mail. If you do not understand it lemme know.

One thing more before you compile it, just verify the port where your mysql is running. In my case it is running on default port.

Ok I didn't closely examined the mysql stuff sent by you. Your's is also running on 3306 (default port). But you've to change the password to whatever your root password is.

Hi,

I have made some changes to the source code and made it work. You have to do the additional CreditCard related stuff yourself. In any case feel free to drop a message. Find the source in src.zip attached with this mail. If you do not understand it lemme know.

One thing more before you compile it, just verify the port where your mysql is running. In my case it is running on default port.

Cool. Thanks I keep getting "Null pointer" at

stm.createStatement(insert...)

I will look at it. Again. thanks.

3306 do I change

private String connURL="jdbc:mysql://localhost:8080";

with 3306 instead of 8080?
I wanted to find out what

private String dbdriver= "org.gjt.mm.mysql.Driver";

is. I copied and pasted it and I just thought it was a driver with some crazy path I don't understand. Anyway I will look at the .zip.
thanks

Wow. impressive code there. Like, is there a simple change I could make to insert a record ? My professor would fall out of his chair if I laid all that on him. I would have to tear that code apart to understand it yet at my first look it looks quiet through. If you recommend this as the best way to do it don't sweat it I will have to wait until I can study that one.
thanks
Have a good one.

Ok ! I thought you wanted a reusable bean so I made the changes that way.

However, I think you are missing to put the mysql jdbc driver in the classpath of your program.
You can download it from the following link : http://mysql.tonnikala.org/Downloads/Connector-J/mysql-connector-java-5.0.7.tar.gz

Unzip it and you will find a mysql-connector-java-5.0.7.jar file. This jar contains the Drivers which will connect to the mySQL database.

As you asked you wanted to know about

private String dbdriver= "org.gjt.mm.mysql.Driver";

, it is one of the drivers which will help your java application to communicate to mysql database.

There are other drivers also available in the mysql-connector-java-5.0.7.jar file. I prefer using "com.mysql.jdbc.Driver".

You remember the code where you say

Class.forName("com.mysql.jdbc.Driver"); or Class.forName("org.gjt.mm.mysql.Driver");

This is where you are loading mysql driver into the JVM which will communicate with the underlying database. ccdb in your case.

Do not change the port no. Keep it to 3306 because it is the port where mySQL is running.

I suppose that you are also using Tomcat because you are messing up the port of mysql(3306) with that of Tomcat(8080).

thank you very much it is getting closer to working and then I can mess with it to see how it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.