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;
}
}
java beginner jdbc
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.
add a comment |
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;
}
}
java beginner jdbc
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.
add a comment |
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;
}
}
java beginner jdbc
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
java beginner jdbc
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.
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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