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","[email protected]");
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 = null;
Class.forName("org.postgresql.Driver");
connect = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"+this.dbName,this.dbUsername,this.dbPassword);
if( connect == null ) return false;
return true;
}
public void setDatabaseInfo(String a,String b,String c)
{
this.dbName = a;
this.dbUsername = b;
this.dbPassword = c;
}
public Registeration()
{
//Random random = new Random();
id = 0; // random.nextInt(2000);
usr = null;
pwd = null;
email = null;
}
public Registeration(String usr,String pwd,String email)
{
Random random = new Random();
this.id = random.nextInt(1000);
this.usr = usr;
this.pwd = pwd;
this.email = email;
}
}
In my view this code is horrible. How can I develop this code?