patternjavaMinor
Using PostgreSQL effectively with a banking app
Viewed 0 times
postgresqlappwitheffectivelybankingusing
Problem
I'm kind of newbie for these things but I did something and I want to know how I can do this effectively.
```
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Random;
public class BankingApp
{
public static void main(String[] args)
{
Registeration myreg = new Registeration("vivian","stuff","root@root.org");
myreg.setDatabaseInfo("JavaAppFirst","root","134679");
myreg.beRegister();
}
}
interface SQLConnection
{
public boolean setConnection() throws Exception; // implement the sql connection
// public void closeConnection(Connection c,Statement stmt) throws SQLException; // close connection
}
class Registeration implements SQLConnection
{
private int id;
private String usr,pwd,email;
private Connection connect;
private Statement state;
private String dbName,dbUsername,dbPassword;
private void tempRegister() throws Exception
{
connect.setAutoCommit(false);
state = connect.createStatement();
String sqlQuery = "INSERT INTO users (id,uname,upass,uemail) VALUES ('"+this.id+"','"+this.usr+"','"+this.pwd+"','"+this.email+"');";
state.executeUpdate(sqlQuery);
state.close();
connect.commit();
connect.close();
}
public void beRegister()
{
try
{
if( setConnection() != true )
{
System.err.println("[+] Connection Error");
return;
}
tempRegister();
System.out.println("[+] Registeration Successfully");
}catch(Exception e)
{
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
public boolean setConnection() throws Exception
{
connect = null;
state = nul
```
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Random;
public class BankingApp
{
public static void main(String[] args)
{
Registeration myreg = new Registeration("vivian","stuff","root@root.org");
myreg.setDatabaseInfo("JavaAppFirst","root","134679");
myreg.beRegister();
}
}
interface SQLConnection
{
public boolean setConnection() throws Exception; // implement the sql connection
// public void closeConnection(Connection c,Statement stmt) throws SQLException; // close connection
}
class Registeration implements SQLConnection
{
private int id;
private String usr,pwd,email;
private Connection connect;
private Statement state;
private String dbName,dbUsername,dbPassword;
private void tempRegister() throws Exception
{
connect.setAutoCommit(false);
state = connect.createStatement();
String sqlQuery = "INSERT INTO users (id,uname,upass,uemail) VALUES ('"+this.id+"','"+this.usr+"','"+this.pwd+"','"+this.email+"');";
state.executeUpdate(sqlQuery);
state.close();
connect.commit();
connect.close();
}
public void beRegister()
{
try
{
if( setConnection() != true )
{
System.err.println("[+] Connection Error");
return;
}
tempRegister();
System.out.println("[+] Registeration Successfully");
}catch(Exception e)
{
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
public boolean setConnection() throws Exception
{
connect = null;
state = nul
Solution
this looks really ugly from a readability/formatting point of view.
You should use consistent Egyptian (Java) bracing for this along with making sure that you leave some space around the Braces and not around the parenthesis.
Key words also like their space from parenthesis.
Look how much cleaner this little piece of code looks now.
public void beRegister()
{
try
{
if( setConnection() != true )
{
System.err.println("[+] Connection Error");
return;
}
tempRegister();
System.out.println("[+] Registeration Successfully");
}catch(Exception e)
{
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}You should use consistent Egyptian (Java) bracing for this along with making sure that you leave some space around the Braces and not around the parenthesis.
Key words also like their space from parenthesis.
Look how much cleaner this little piece of code looks now.
public void beRegister() {
try {
if (setConnection() != true) {
System.err.println("[+] Connection Error");
return;
}
tempRegister();
System.out.println("[+] Registeration Successfully");
} catch (Exception e) {
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}Code Snippets
public void beRegister()
{
try
{
if( setConnection() != true )
{
System.err.println("[+] Connection Error");
return;
}
tempRegister();
System.out.println("[+] Registeration Successfully");
}catch(Exception e)
{
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}public void beRegister() {
try {
if (setConnection() != true) {
System.err.println("[+] Connection Error");
return;
}
tempRegister();
System.out.println("[+] Registeration Successfully");
} catch (Exception e) {
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}Context
StackExchange Code Review Q#71800, answer score: 3
Revisions (0)
No revisions yet.