Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: JDBC database connection problem

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JDBC database connection problem

    Hi pls can someone help me solve a problem with the connection to a JDBC database?

    I created a app with database connection and used the next code:

     Connection conn = DriverManager.getConnection("jdbc:firebirdsql://localhost:3050/c:/service/db/service.fdb?encoding=ISO8859_1;autoReconnect=true&allowMultiQueries=true","sysdba","masterkey");
     System.out.println("Conectat");

    the code is ok but i need some solution to get the url(c:/service/db/service.fdb). Until now my solution is to try to find somehow to set a JLabel or TextField with that value of the url but this works until i
    restart the app because after that the Label or the TextField is empty.

    Is there a way to set a Label or TextField to get the string from a jfilechooser but to be permanent not to reset when i restart the app? The problem with this is that i need just once i want to use someting like this:

    conn = DriverManager.getConnection("jdbc:firebirdsql://localhost:3050/"+path_db.getText()+"?encoding=ISO8859_1;autoReconnect=true&allowMultiQueries=true","sysdba","masterkey");
    System.out.println("Conectat");


    thanks in advance for answers

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JDBC database connection problem

    to be permanent not to reset when i restart the app?
    The value could be saved to a disk file that could be read each time the app starts execution.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JDBC database connection problem

    is a solution i manage only to create the file. i dont know how to get the string back from file

    this is the cod for the text file:
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
               try {
                String str = cale_db.getText();
                File newTextFile = new File("C:/thetextfile.txt");
     
                FileWriter fw = new FileWriter(newTextFile);
                fw.write(str);
                fw.close();
     
            } catch (IOException iox) {
                //do stuff with exception
                iox.printStackTrace();
            }
    Last edited by cardmarius; February 4th, 2018 at 04:19 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JDBC database connection problem

    how to get the string back from file
    For a simple file, the Scanner class has methods that can read its contents into a String.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JDBC database connection problem

    hi tnx for your help i manage to get the string from a txt file to a testfield if thie helps someone ill post the code and in this form you can find the solution

    private void text() {
            BufferedReader br = null;
    		try {
    			br = new BufferedReader(new FileReader("C:\\cale.txt"));
    			String line;
     
    			while ((line = br.readLine()) != null) {
    				cale_db.setText(line);
    			}
     
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				br.close();
    			} catch (IOException e) {
     
    				e.printStackTrace();
    			}
    		}
        }

  6. #6

    Default Re: JDBC database connection problem

    If you are connecting with service name you should put / before service name

    jdbc:oracle:thin:@hostname:port/service_name

    if you are connecting with sid you should put : before sid

    jdbc:oracle:thin:@hostname:port:sid

    or use the description in your tns file after @

    jdbc:oracle:thin:@(DESCRIPTION=....)
    Last edited by Norm; February 15th, 2018 at 07:20 AM. Reason: Remove smileys

Similar Threads

  1. Replies: 1
    Last Post: August 21st, 2014, 01:49 PM
  2. Replies: 0
    Last Post: August 21st, 2014, 06:25 AM
  3. Problem with CRUD connection to database, help? :S
    By Issuee in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 7th, 2014, 10:56 AM
  4. jdbc connection problem
    By sny in forum JDBC & Databases
    Replies: 11
    Last Post: January 6th, 2011, 04:39 AM