943,965 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1239
  • Java RSS
Sep 16th, 2007
0

connection mysql

Expand Post »
I would like to return a connection to a Bean to make statements to mysql database "ccdb" I'm really not sure of
Java Syntax (Toggle Plain Text)
  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?
Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 7
Solved Threads: 2
Posting Pro
ceyesuma is offline Offline
524 posts
since Aug 2007
Sep 16th, 2007
0

Re: connection mysql

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");
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Sep 16th, 2007
0

Re: connection mysql

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??
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Sep 16th, 2007
0

Re: connection mysql new update

Java Syntax (Toggle Plain Text)
  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;
}

}
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 2
Posting Pro
ceyesuma is offline Offline
524 posts
since Aug 2007
Sep 16th, 2007
0

Re: connection mysql

Your code doesn't seem to be correct. It should be something thing like:
Java Syntax (Toggle Plain Text)
  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]
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Sep 16th, 2007
0

Re: connection mysql

Click to Expand / Collapse  Quote originally posted by lookof2day ...
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?
Reputation Points: 7
Solved Threads: 2
Posting Pro
ceyesuma is offline Offline
524 posts
since Aug 2007
Sep 16th, 2007
0

Re: connection mysql

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
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007
Sep 16th, 2007
0

Re: connection mysql

ok thanks.
Reputation Points: 7
Solved Threads: 2
Posting Pro
ceyesuma is offline Offline
524 posts
since Aug 2007
Sep 16th, 2007
0

Re: connection mysql

Java Syntax (Toggle Plain Text)
  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

Java Syntax (Toggle Plain Text)
  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!
Reputation Points: 7
Solved Threads: 2
Posting Pro
ceyesuma is offline Offline
524 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Extract field from text file
Next Thread in Java Forum Timeline: Rusty at Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC