Beginning to work with Derby











up vote
0
down vote

favorite












I have the following code that seems to work. I'm just wondering if I'm doing everything right or if there are some obvious improvements that should be made.



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;

public class DBtest{
private static int cnt = 0;
public static void main(String args) throws SQLException{
System.out.print('u000C');
String db = "test2";
File file = new File(db);

Connection conn = connectDB(db);
close(conn);
deleteDB(file);

System.exit(0);
}
public static void close(Connection conn){
try{
if (conn != null) conn.close();
System.out.println("Connection closed");
}catch(SQLException e){
System.out.println("connection NOT closed " + e);
System.exit(0);
}
}

public static void deleteDB(File file) {
try{
DriverManager.getConnection("jdbc:derby:;shutdown=true");
}catch(SQLException e1){
if(e1.getSQLState().equals("XJ015") && e1.getErrorCode() == 50000){
try{
FileUtils.deleteDirectory(file);
}catch(IOException e2){
System.out.println("FATAL ERROR: folder not deleted " + e2);
System.exit(0);
}
System.out.println("Database shutdown successful");
}else{
System.out.println("FATAL ERROR: Database not shutdown " + e1);
System.exit(0);
}
}
}

public static Connection connectDB(String database) {
Connection conn = null;
try{
File db = new File(database);
String URL = "jdbc:derby:" + database + ";create=true";
System.out.println("db exists " + db.exists());

conn = DriverManager.getConnection(URL);
System.out.println("Succesfully connected to " + database);
}catch(SQLException e){
System.out.println("FATAL ERROR: from getDB " + e);
System.exit(0);
}
return conn;
}
}









share|improve this question
















bumped to the homepage by Community 25 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    0
    down vote

    favorite












    I have the following code that seems to work. I'm just wondering if I'm doing everything right or if there are some obvious improvements that should be made.



    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import org.apache.commons.io.FileUtils;
    import java.io.File;
    import java.io.IOException;

    public class DBtest{
    private static int cnt = 0;
    public static void main(String args) throws SQLException{
    System.out.print('u000C');
    String db = "test2";
    File file = new File(db);

    Connection conn = connectDB(db);
    close(conn);
    deleteDB(file);

    System.exit(0);
    }
    public static void close(Connection conn){
    try{
    if (conn != null) conn.close();
    System.out.println("Connection closed");
    }catch(SQLException e){
    System.out.println("connection NOT closed " + e);
    System.exit(0);
    }
    }

    public static void deleteDB(File file) {
    try{
    DriverManager.getConnection("jdbc:derby:;shutdown=true");
    }catch(SQLException e1){
    if(e1.getSQLState().equals("XJ015") && e1.getErrorCode() == 50000){
    try{
    FileUtils.deleteDirectory(file);
    }catch(IOException e2){
    System.out.println("FATAL ERROR: folder not deleted " + e2);
    System.exit(0);
    }
    System.out.println("Database shutdown successful");
    }else{
    System.out.println("FATAL ERROR: Database not shutdown " + e1);
    System.exit(0);
    }
    }
    }

    public static Connection connectDB(String database) {
    Connection conn = null;
    try{
    File db = new File(database);
    String URL = "jdbc:derby:" + database + ";create=true";
    System.out.println("db exists " + db.exists());

    conn = DriverManager.getConnection(URL);
    System.out.println("Succesfully connected to " + database);
    }catch(SQLException e){
    System.out.println("FATAL ERROR: from getDB " + e);
    System.exit(0);
    }
    return conn;
    }
    }









    share|improve this question
















    bumped to the homepage by Community 25 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have the following code that seems to work. I'm just wondering if I'm doing everything right or if there are some obvious improvements that should be made.



      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.SQLException;
      import java.sql.Statement;
      import org.apache.commons.io.FileUtils;
      import java.io.File;
      import java.io.IOException;

      public class DBtest{
      private static int cnt = 0;
      public static void main(String args) throws SQLException{
      System.out.print('u000C');
      String db = "test2";
      File file = new File(db);

      Connection conn = connectDB(db);
      close(conn);
      deleteDB(file);

      System.exit(0);
      }
      public static void close(Connection conn){
      try{
      if (conn != null) conn.close();
      System.out.println("Connection closed");
      }catch(SQLException e){
      System.out.println("connection NOT closed " + e);
      System.exit(0);
      }
      }

      public static void deleteDB(File file) {
      try{
      DriverManager.getConnection("jdbc:derby:;shutdown=true");
      }catch(SQLException e1){
      if(e1.getSQLState().equals("XJ015") && e1.getErrorCode() == 50000){
      try{
      FileUtils.deleteDirectory(file);
      }catch(IOException e2){
      System.out.println("FATAL ERROR: folder not deleted " + e2);
      System.exit(0);
      }
      System.out.println("Database shutdown successful");
      }else{
      System.out.println("FATAL ERROR: Database not shutdown " + e1);
      System.exit(0);
      }
      }
      }

      public static Connection connectDB(String database) {
      Connection conn = null;
      try{
      File db = new File(database);
      String URL = "jdbc:derby:" + database + ";create=true";
      System.out.println("db exists " + db.exists());

      conn = DriverManager.getConnection(URL);
      System.out.println("Succesfully connected to " + database);
      }catch(SQLException e){
      System.out.println("FATAL ERROR: from getDB " + e);
      System.exit(0);
      }
      return conn;
      }
      }









      share|improve this question















      I have the following code that seems to work. I'm just wondering if I'm doing everything right or if there are some obvious improvements that should be made.



      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.SQLException;
      import java.sql.Statement;
      import org.apache.commons.io.FileUtils;
      import java.io.File;
      import java.io.IOException;

      public class DBtest{
      private static int cnt = 0;
      public static void main(String args) throws SQLException{
      System.out.print('u000C');
      String db = "test2";
      File file = new File(db);

      Connection conn = connectDB(db);
      close(conn);
      deleteDB(file);

      System.exit(0);
      }
      public static void close(Connection conn){
      try{
      if (conn != null) conn.close();
      System.out.println("Connection closed");
      }catch(SQLException e){
      System.out.println("connection NOT closed " + e);
      System.exit(0);
      }
      }

      public static void deleteDB(File file) {
      try{
      DriverManager.getConnection("jdbc:derby:;shutdown=true");
      }catch(SQLException e1){
      if(e1.getSQLState().equals("XJ015") && e1.getErrorCode() == 50000){
      try{
      FileUtils.deleteDirectory(file);
      }catch(IOException e2){
      System.out.println("FATAL ERROR: folder not deleted " + e2);
      System.exit(0);
      }
      System.out.println("Database shutdown successful");
      }else{
      System.out.println("FATAL ERROR: Database not shutdown " + e1);
      System.exit(0);
      }
      }
      }

      public static Connection connectDB(String database) {
      Connection conn = null;
      try{
      File db = new File(database);
      String URL = "jdbc:derby:" + database + ";create=true";
      System.out.println("db exists " + db.exists());

      conn = DriverManager.getConnection(URL);
      System.out.println("Succesfully connected to " + database);
      }catch(SQLException e){
      System.out.println("FATAL ERROR: from getDB " + e);
      System.exit(0);
      }
      return conn;
      }
      }






      java beginner jdbc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 1 at 23:58









      200_success

      127k15149412




      127k15149412










      asked Nov 1 at 21:29









      DCR

      1063




      1063





      bumped to the homepage by Community 25 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 25 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Instead of appending strings with +, I strongly recommend you use String.format() to format your strings, especially when dealing with sensitive strings like database connections.



          You can view documentation about it here: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html



          Another piece of advise - avoid "magic numbers" or "magic strings". You should not have random strings or numbers in your code which the reader of the code doesn't know the meaning of.



          Some examples are: 50000 or XJ015.



          These should be defined specifically as static final strings in your Java file, making it much easier to understand what it signifies.



          As a last piece of advice, use System.out.err for errors rather than println. This allows you to see it in red in the console, being able to more easily identify an error.






          share|improve this answer





















          • Thanks for your comments. Could you explain why you strongly recommend using String.format()?
            – DCR
            Nov 4 at 16:22










          • Generally, string concatenation should be prefered over String.format. The latter has two main disadvantages: It does not encode the string to be built in a local manner. The building process is encoded in a string.
            – DCR
            Nov 4 at 16:27













          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206757%2fbeginning-to-work-with-derby%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Instead of appending strings with +, I strongly recommend you use String.format() to format your strings, especially when dealing with sensitive strings like database connections.



          You can view documentation about it here: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html



          Another piece of advise - avoid "magic numbers" or "magic strings". You should not have random strings or numbers in your code which the reader of the code doesn't know the meaning of.



          Some examples are: 50000 or XJ015.



          These should be defined specifically as static final strings in your Java file, making it much easier to understand what it signifies.



          As a last piece of advice, use System.out.err for errors rather than println. This allows you to see it in red in the console, being able to more easily identify an error.






          share|improve this answer





















          • Thanks for your comments. Could you explain why you strongly recommend using String.format()?
            – DCR
            Nov 4 at 16:22










          • Generally, string concatenation should be prefered over String.format. The latter has two main disadvantages: It does not encode the string to be built in a local manner. The building process is encoded in a string.
            – DCR
            Nov 4 at 16:27

















          up vote
          0
          down vote













          Instead of appending strings with +, I strongly recommend you use String.format() to format your strings, especially when dealing with sensitive strings like database connections.



          You can view documentation about it here: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html



          Another piece of advise - avoid "magic numbers" or "magic strings". You should not have random strings or numbers in your code which the reader of the code doesn't know the meaning of.



          Some examples are: 50000 or XJ015.



          These should be defined specifically as static final strings in your Java file, making it much easier to understand what it signifies.



          As a last piece of advice, use System.out.err for errors rather than println. This allows you to see it in red in the console, being able to more easily identify an error.






          share|improve this answer





















          • Thanks for your comments. Could you explain why you strongly recommend using String.format()?
            – DCR
            Nov 4 at 16:22










          • Generally, string concatenation should be prefered over String.format. The latter has two main disadvantages: It does not encode the string to be built in a local manner. The building process is encoded in a string.
            – DCR
            Nov 4 at 16:27















          up vote
          0
          down vote










          up vote
          0
          down vote









          Instead of appending strings with +, I strongly recommend you use String.format() to format your strings, especially when dealing with sensitive strings like database connections.



          You can view documentation about it here: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html



          Another piece of advise - avoid "magic numbers" or "magic strings". You should not have random strings or numbers in your code which the reader of the code doesn't know the meaning of.



          Some examples are: 50000 or XJ015.



          These should be defined specifically as static final strings in your Java file, making it much easier to understand what it signifies.



          As a last piece of advice, use System.out.err for errors rather than println. This allows you to see it in red in the console, being able to more easily identify an error.






          share|improve this answer












          Instead of appending strings with +, I strongly recommend you use String.format() to format your strings, especially when dealing with sensitive strings like database connections.



          You can view documentation about it here: https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html



          Another piece of advise - avoid "magic numbers" or "magic strings". You should not have random strings or numbers in your code which the reader of the code doesn't know the meaning of.



          Some examples are: 50000 or XJ015.



          These should be defined specifically as static final strings in your Java file, making it much easier to understand what it signifies.



          As a last piece of advice, use System.out.err for errors rather than println. This allows you to see it in red in the console, being able to more easily identify an error.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 4 at 7:09









          Faraz

          27019




          27019












          • Thanks for your comments. Could you explain why you strongly recommend using String.format()?
            – DCR
            Nov 4 at 16:22










          • Generally, string concatenation should be prefered over String.format. The latter has two main disadvantages: It does not encode the string to be built in a local manner. The building process is encoded in a string.
            – DCR
            Nov 4 at 16:27




















          • Thanks for your comments. Could you explain why you strongly recommend using String.format()?
            – DCR
            Nov 4 at 16:22










          • Generally, string concatenation should be prefered over String.format. The latter has two main disadvantages: It does not encode the string to be built in a local manner. The building process is encoded in a string.
            – DCR
            Nov 4 at 16:27


















          Thanks for your comments. Could you explain why you strongly recommend using String.format()?
          – DCR
          Nov 4 at 16:22




          Thanks for your comments. Could you explain why you strongly recommend using String.format()?
          – DCR
          Nov 4 at 16:22












          Generally, string concatenation should be prefered over String.format. The latter has two main disadvantages: It does not encode the string to be built in a local manner. The building process is encoded in a string.
          – DCR
          Nov 4 at 16:27






          Generally, string concatenation should be prefered over String.format. The latter has two main disadvantages: It does not encode the string to be built in a local manner. The building process is encoded in a string.
          – DCR
          Nov 4 at 16:27




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206757%2fbeginning-to-work-with-derby%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'