connection mysql

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

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

connection mysql

 
0
  #1
Sep 16th, 2007
I would like to return a connection to a Bean to make statements to mysql database "ccdb" I'm really not sure of
  1. public static Connection getConnection(){
  2. if(connection==null){
  3. try{
  4. Class.forName("org.gjt.mm.mysql.Driver");
  5. connection=DriverManager.getConnection("jdbc:mysql://localhost:8084","root","ceyesuma");
  6. }catch(Exception exc){ connection=null;}
  7. }
  8.  
  9. return connection;
  10. }

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

Re: connection mysql

 
0
  #2
Sep 16th, 2007
You are required to mention the database name too which u r missing. Jst add the db name as given below

try{
Class.forName("org.gjt.mm.mysql.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:8084/ccdb","root","ceyesuma");
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: connection mysql

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

Re: connection mysql new update

 
0
  #4
Sep 16th, 2007
  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. }
[code]
/*
* 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;
}

}
  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. }
Last edited by ceyesuma; Sep 16th, 2007 at 10:26 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: connection mysql

 
0
  #5
Sep 16th, 2007
Your code doesn't seem to be correct. It should be something thing like:
  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. private String connURL="jdbc:mysql://localhost:8084";
  11. private String dbusername="root";
  12. private String dbpassword="ceyesuma";
  13. private String dbdriver= "org.gjt.mm.mysql.Driver";
  14. private String dbname = "ccdb";
  15. private static DBConnection connection=null;
  16. /** Creates a new instance of DBConnection */
  17. private DBConnection() {
  18. }
  19. public String getDbdriver(){
  20. return dbdriver;
  21. }
  22. public String getConnURL(){
  23. return connURL;
  24. }
  25. public String getDbusername(){
  26. return dbusername;
  27. }
  28. public String getDbpassword(){
  29. return dbpassword;
  30. }
  31. public void setDbdriver(String dbdriver){
  32. this.dbdriver=dbdriver;
  33. }
  34. public void setConnURL(String url){
  35. this.connURL=url;
  36. }
  37. public void setDbusername(String dbusername){
  38. this.dbusername=dbusername;
  39. }
  40. public void setDbpassword(String dbpassword){
  41. this.dbpassword=dbpassword;
  42. }
  43. public void setDbname(String dbname){
  44. this.dbname = dbname;
  45. }
  46. public String getDbname(){
  47. return dbname;
  48. }
  49. public static DBConnection getInstance(){
  50. if(connection==null){
  51. connection = new DBConnection();
  52. }
  53. return connection;
  54. }
  55. public Connection getConnection() throws Exception{
  56. Connection connection = null;
  57. Class.forName(driver);
  58. connection = DriverManager.getConnection(connURL +"/" + dbname, dbusername, dbpassword);
  59. return connection;
  60. }
  61. }
[/QUOTE]
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 242
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

Re: connection mysql

 
0
  #6
Sep 16th, 2007
Originally Posted by lookof2day View Post
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 update thread?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 81
Reputation: lookof2day is an unknown quantity at this point 
Solved Threads: 10
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: connection mysql

 
0
  #7
Sep 16th, 2007
You are making the same mistake. You are not giving the database name in your URL. Your URL should be jdbc:mysql://localhost:8084/ccbd
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 242
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

Re: connection mysql

 
0
  #8
Sep 16th, 2007
ok thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 242
Reputation: ceyesuma is an unknown quantity at this point 
Solved Threads: 0
ceyesuma ceyesuma is offline Offline
Posting Whiz in Training

Re: connection mysql

 
0
  #9
Sep 16th, 2007
  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!
Reply With Quote Quick reply to this message  
Reply

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




Views: 1116 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC