mock credit card with mysql & filewriter

Thread Solved

Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

mock credit card with mysql & filewriter

 
0
  #1
Sep 16th, 2007
  1.  
  2. public static Connection getConnection(){
  3. if(connection==null){
  4. try{
  5. Class.forName("org.gjt.mm.mysql.Driver");
  6. connection=DriverManager.getConnection("jdbc:mysql://localhost:8084","root","ceyesuma");
  7. }catch(Exception exc){ connection=null;}
  8. }
  9.  
  10. return connection;
  11. }

Here is the bean to get connected. Should this work?
  1. package db;
  2. import java.sql.*;
  3. import java.sql.DriverManager;
  4. import java.sql.Connection;
  5. /**
  6.  *
  7.  * @author James
  8.  */
  9. public class DBConnection {
  10. static String dbdriver;
  11. static String connURL;
  12. static String dbusername="";
  13. static String dbpassword="";
  14. static Connection connection=null;
  15. /** Creates a new instance of DBConnection */
  16. public DBConnection() {
  17. }
  18. public String getDbdriver(){
  19. return dbdriver;
  20. }
  21. public String getConnURL(){
  22. return connURL;
  23. }
  24. public String getDbusername(){
  25. return dbusername;
  26. }
  27. public String getDbpassword(){
  28. return dbpassword;
  29. }
  30. public void setDbdriver(String dbdriver){
  31. DBConnection.dbdriver=dbdriver;
  32. }
  33. public void setConnURL(String url){
  34. DBConnection.connURL=url;
  35. }
  36. public void setDbusername(String dbusername){
  37. DBConnection.dbusername=dbusername;
  38. }
  39. public void setDbpassword(String dbpassword){
  40. DBConnection.dbpassword=dbpassword;
  41. }
  42. public static Connection getConnection(){
  43. if(connection==null){
  44. try{
  45. Class.forName("org.gjt.mm.mysql.Driver");
  46. connection=DriverManager.getConnection("jdbc:mysql://localhost:8084","root","ceyesuma");
  47. }catch(Exception exc){ connection=null;}
  48. }
  49.  
  50. return connection;
  51. }
  52.  
  53. }

  1. package creditcard;
  2. import db.DBConnection;
  3. import java.lang.Object.*;
  4. import java.io.*;
  5. import java.sql.*;
  6.  
  7. import java.util.Date;
  8. import tio.*;
  9. import java.io.File.*;
  10. import java.io.IOException;
  11.  
  12.  
  13. class CreditCard {
  14. private long AccountNo;
  15. private double CreditLimit;
  16. public double TotalCharges;
  17. public double TotalPayments;
  18. private double Amount;
  19. private int TransResult;
  20. private String TransErr;
  21. private String Description;
  22. private String log=null;
  23. private String date;
  24. private FormattedWriter out;
  25.  
  26. private Statement stm = null;
  27. private ResultSet rst = null;
  28. private String tableName = "acctdata";
  29. private String x;
  30. private String query;
  31. private DBConnection db;
  32.  
  33. private Connection Conn;
  34.  
  35. public CreditCard( ) throws FileNotFoundException, IOException {
  36. newAccount();
  37. CreditLimit = 1000;
  38. TotalCharges = 0;
  39. TotalPayments =0;
  40.  
  41. }
  42.  
  43. public CreditCard(long AccountNo) throws FileNotFoundException, IOException {
  44. this.AccountNo = AccountNo;
  45. CreditLimit = 1000;
  46. TotalCharges = 0;
  47. TotalPayments =0;
  48. //validate AccountNO
  49.  
  50. }
  51. private void accountStatus() throws IOException{
  52.  
  53. String f = "CC_Num_status.txt";
  54. File file = new File(f);
  55. FormattedWriter fw=new FormattedWriter(f);
  56. if(f!=null){
  57.  
  58. fw.printfln("Account: " + accountNumber());
  59. fw.printfln("Credit Limit: " + creditLimit());
  60. fw.printfln("Available Credit: " + Available());
  61. fw.printfln("Outstanding Balance: " + Balance());
  62. fw.printfln("Charge: " + Amount);
  63. fw.printfln("Description; " + Description);
  64. fw.printfln("payment: " + Amount);
  65. fw.printfln("Total Charges: " + TotalCharges);
  66. fw.printfln("Total Payments " + TotalPayments);
  67. //fw.printfln("\n");
  68. // fw.printfln("Transaction (0=Quit, +$=charge, -$=payment, 9999=Limit increase): ");
  69.  
  70. }
  71. }
  72. private void newAccount() throws FileNotFoundException, IOException {
  73. double r = Math.random();
  74. AccountNo = (long)(r * 100000000);
  75.  
  76. }
  77.  
  78. public long accountNumber() {
  79. return AccountNo;
  80. }
  81.  
  82.  
  83.  
  84. private void writeLog(double Amount, String Description) throws FileNotFoundException, IOException, SQLException {
  85. this.AccountNo = AccountNo;
  86. Date dt = new Date();
  87. dt.getTime();
  88. String f = "CC" + AccountNo + ".txt";
  89. File log = new File(f);
  90. FormattedWriter out = new FormattedWriter(new FileWriter(log,true));
  91. out.printfln(AccountNo);
  92. out.printfln(dt.toString());
  93. out.printfln(Amount);
  94. out.printfln(Description);
  95.  
  96. out.close();
  97.  
  98. setData();
  99.  
  100. }
  101.  
  102.  
  103.  
  104. public String getTableName(){
  105. return tableName;
  106. }
  107. public void setTableName(String tableName){
  108. this.tableName = tableName;
  109. }
  110.  
  111. public String getData() throws SQLException{
  112. if(tableName==null || tableName.equals("")){
  113. return "";
  114. }
  115.  
  116. return x;
  117. }
  118.  
  119. public void setData() throws SQLException{
  120. if(tableName==null || tableName.equals("")){
  121. return;
  122. }
  123. Connection conn =null;
  124. db.getConnection();
  125. String query = "INSERT INTO"+tableName+" VALUES(11111112,'2000-7-3',50,'THIS',25";
  126. Statement stm= conn.createStatement();
  127. stm.executeUpdate(query);
  128. }
  129. public double creditLimit() {
  130. return CreditLimit;
  131. }
  132.  
  133. public void creditIncrease()throws FileNotFoundException, IOException{
  134. TransResult = 0;
  135. if (Math.random() > .25) {
  136. CreditLimit += 100;
  137. accountStatus();
  138.  
  139. } else {
  140. System.out.println("Sorry, credit increase not possible at this time.");
  141. TransResult = 1;
  142. }
  143. }
  144. public double Available() {
  145. double Available = CreditLimit - ( TotalCharges - TotalPayments );
  146. return Available;
  147. }
  148.  
  149. public double Balance() {
  150. double Balance = ( TotalCharges - TotalPayments );
  151. return Balance;
  152. }
  153. public void Transaction( double Amount, String Description ) throws FileNotFoundException, IOException, SQLException {
  154.  
  155. TransResult = 0;
  156.  
  157. if ( Amount == 0 ) {
  158. TransResult = 1;
  159. TransErr = "Transaction amount is 0.";
  160. return;
  161. }
  162.  
  163.  
  164. if ( Amount > Available() ) {
  165. TransResult = 1;
  166. TransErr = "Transaction amount of $" + Amount + " has exceeded the available credit limit $" + Available();
  167. return;
  168. }
  169.  
  170.  
  171. if ( Description == "" ) {
  172. TransResult = 1;
  173. TransErr = "No transaction description entered.";
  174. return;
  175. }
  176. if ( Amount > 0 ) {
  177.  
  178. TotalCharges += Amount;
  179. accountStatus();
  180. writeLog(Amount, Description);
  181.  
  182.  
  183. } else if ( Amount < 0 ) {
  184.  
  185. TotalPayments += -(Amount);
  186. accountStatus();
  187. writeLog(Amount, Description);
  188.  
  189. }
  190. }
  191.  
  192.  
  193.  
  194.  
  195. }

  1. /*
  2. * DBConnection.java
  3. *
  4. * Created on September 11, 2007, 11:50 AM
  5. *
  6. * To change this template, choose Tools | Template Manager
  7. * and open the template in the editor.
  8. */
  9.  
  10. package db;
  11. import java.sql.*;
  12. import java.sql.DriverManager;
  13. import java.sql.Connection;
  14. /**
  15. *
  16. * @author James
  17. */
  18. public class DBConnection {
  19. static String dbdriver;
  20. static String connURL;
  21. static String dbusername="";
  22. static String dbpassword="";
  23. static Connection connection=null;
  24. /** Creates a new instance of DBConnection */
  25. public DBConnection() {
  26. }
  27. public String getDbdriver(){
  28. return dbdriver;
  29. }
  30. public String getConnURL(){
  31. return connURL;
  32. }
  33. public String getDbusername(){
  34. return dbusername;
  35. }
  36. public String getDbpassword(){
  37. return dbpassword;
  38. }
  39. public void setDbdriver(String dbdriver){
  40. DBConnection.dbdriver=dbdriver;
  41. }
  42. public void setConnURL(String url){
  43. DBConnection.connURL=url;
  44. }
  45. public void setDbusername(String dbusername){
  46. DBConnection.dbusername=dbusername;
  47. }
  48. public void setDbpassword(String dbpassword){
  49. DBConnection.dbpassword=dbpassword;
  50. }
  51. public static Connection getConnection(){
  52. if(connection==null){
  53. try{
  54. Class.forName(dbdriver).newInstance();
  55. Class.forName("org.gjt.mm.mysql.Driver");
  56. connection=DriverManager.getConnection("jdbc:mysql://localhost:8080","root","ceyesuma");
  57. }catch(Exception exc){ connection=null;}
  58. }
  59.  
  60. return connection;
  61. }
  62.  
  63. }
  1. DROP DATABASE IF EXISTS ccdb;
  2. CREATE DATABASE ccdb;
  3. USE ccdb;
  4.  
  5. CREATE TABLE acctData
  6. ( AccountNo long Primary Key,
  7. transdate char(20),
  8. chrg double,
  9. transdesc VARCHAR(100),
  10. pymnt double) );
  11.  
  12. INSERT INTO acctData VALUES (99999999, '2000-7-3',30,'food',50);
Here is the rest of the program. This class test CreditCard
  1. package creditcard;
  2. //Credit Card Test - tests CreditCard class
  3.  
  4.  
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.RandomAccessFile;
  8. import tio.*;
  9.  
  10. class CreditCardTest {
  11.  
  12. public static void main(String[] args) throws IOException {
  13. new CreditCardTest();
  14. }
  15. public CreditCardTest() throws IOException{
  16. int inval;
  17. long AccountNo;
  18. double tranval=-1;
  19. String transdesc = null;
  20. CreditCard cc;
  21. String f = "CC_Num_status.txt";
  22. File file = new File(f);
  23. FormattedWriter fw=new FormattedWriter(f);
  24. System.out.println("Welcome to the Credit Card simulator!");
  25. System.out.println("Existing Account, New Account, or Exit? (Existing Account=1, New Account=2, Exit=0): ");
  26. inval = Console.in.readInt();
  27. if (inval == 0)
  28. return;
  29.  
  30. if (inval == 1) {
  31. System.out.println("Existing Account; enter Account #: ");
  32.  
  33. AccountNo = Console.in.readLong();
  34. if (AccountNo == 0)
  35. return;
  36.  
  37. cc = new CreditCard(AccountNo);
  38.  
  39. } else {
  40. cc = new CreditCard();
  41. }
  42.  
  43. while (tranval != 0) {
  44. System.out.println("Account: " + cc.accountNumber());
  45. System.out.println("Credit Limit: " + cc.creditLimit());
  46. System.out.println("Available Credit: " + cc.Available());
  47. System.out.println("Outstanding Balance: " + cc.Balance());
  48. System.out.println("Charge: " + + tranval);
  49. System.out.println("Description; " + transdesc);
  50. System.out.println("payment: " + tranval);
  51. System.out.println("Total Charges: " + cc.TotalCharges);
  52. System.out.println("Total Payments " + cc.TotalPayments);
  53. System.out.println("\n");
  54. System.out.println("Transaction (0=Quit, +$=charge, -$=payment, 9999=Limit increase): ");
  55. tranval = Console.in.readDouble();
  56.  
  57.  
  58. if (tranval == 0)
  59. break;
  60.  
  61. if (tranval == 9999)
  62. cc.creditIncrease();
  63. else {
  64. if (tranval > 0) {
  65. System.out.println("Transaction description: ");
  66. transdesc = Console.in.readLine();
  67. transdesc = Console.in.readLine();
  68.  
  69. } else
  70. transdesc = "payment";
  71.  
  72. cc.Transaction(tranval, transdesc);
  73.  
  74. }
  75. }
  76. }
  77. }

  1.  
  2. Your code doesn't seem to be correct. It should be something thing like:
  3.  
  4. package db;
  5. import java.sql.*;
  6. import java.sql.DriverManager;
  7. import java.sql.Connection;
  8. /**
  9. *
  10. * @author James
  11. */
  12. public class DBConnection {
  13. private String connURL="jdbc:mysql://localhost:8084";
  14. private String dbusername="root";
  15. private String dbpassword="ceyesuma";
  16. private String dbdriver= "org.gjt.mm.mysql.Driver";
  17. private String dbname = "ccdb";
  18. private static DBConnection connection=null;
  19. /** Creates a new instance of DBConnection */
  20. private DBConnection() {
  21. }
  22. public String getDbdriver(){
  23. return dbdriver;
  24. }
  25. public String getConnURL(){
  26. return connURL;
  27. }
  28. public String getDbusername(){
  29. return dbusername;
  30. }
  31. public String getDbpassword(){
  32. return dbpassword;
  33. }
  34. public void setDbdriver(String dbdriver){
  35. this.dbdriver=dbdriver;
  36. }
  37. public void setConnURL(String url){
  38. this.connURL=url;
  39. }
  40. public void setDbusername(String dbusername){
  41. this.dbusername=dbusername;
  42. }
  43. public void setDbpassword(String dbpassword){
  44. this.dbpassword=dbpassword;
  45. }
  46. public void setDbname(String dbname){
  47. this.dbname = dbname;
  48. }
  49. public String getDbname(){
  50. return dbname;
  51. }
  52. public static DBConnection getInstance(){
  53. if(connection==null){
  54. connection = new DBConnection();
  55. }
  56. return connection;
  57. }
  58. public Connection getConnection() throws Exception{
  59. Connection connection = null;
  60. Class.forName(driver);
  61. connection = DriverManager.getConnection(connURL +"/" + dbname, dbusername, dbpassword);
  62. return connection;
  63. }
  64. }
  65.  

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

  1. 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

  1. Connection id: 2
  2. Current database: ccdb
  3. Current user: root@localhost
  4. SSL: Not in use
  5. Using delimiter: ;
  6. Server version: 5.0.41-community-nt MySQL Community Edition (GPL)
  7. Protocol version: 10
  8. Connection: localhost via TCP/IP
  9. Server characterset: latin1
  10. Db characterset: latin1
  11. Client characterset: latin1
  12. Conn. characterset: latin1
  13. TCP port: 3306
  14. Uptime: 7 hours 33 min 56 sec
  15.  
  16. Threads: 1 Questions: 17 Slow queries: 0 Opens: 15 Flush tables: 1 Open tab
  17. les: 0 Queries per second avg: 0.001
  18. --------------
  19.  
  20. mysql>
Thanks
GO RAMS!
Last edited by ceyesuma; Sep 16th, 2007 at 4:08 pm. Reason: missing [code]
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 80
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: mock credit card with mysql & filewriter

 
0
  #2
Sep 17th, 2007
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.
Attached Files
File Type: zip src.zip (8.1 KB, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 80
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: mock credit card with mysql & filewriter

 
0
  #3
Sep 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

Re: mock credit card with mysql & filewriter

 
0
  #4
Sep 17th, 2007
Originally Posted by lookof2day View Post
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
  1. stm.createStatement(insert...)
I will look at it. Again. thanks.

3306 do I change
  1. private String connURL="jdbc:mysql://localhost:8080";
with 3306 instead of 8080?
I wanted to find out what
  1. 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
Last edited by ceyesuma; Sep 17th, 2007 at 1:03 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

Re: mock credit card with mysql & filewriter

 
0
  #5
Sep 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 80
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: mock credit card with mysql & filewriter

 
0
  #6
Sep 17th, 2007
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...a-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
  1. 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
  1. 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).
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 238
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

Re: mock credit card with mysql & filewriter

 
0
  #7
Sep 17th, 2007
thank you very much it is getting closer to working and then I can mess with it to see how it works.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC