THIS IS A NO GUI TEST. GUI COMES AFTER THIS IS DONE. " i hope to learn from this so i can use it on the gui " :D
i have my database : friends , table : info , values : (name ) : dragon , (address) : china
i have no idea how to do the password and user name check on this.
-------------------------------- MY Main class ------------ or tester -----------------------
package Main;
import java.util.*;
import java.sql.*;
import DBAccess.DBUtil;
/**
*
* @author christian @modified by moraxus
*/
public class Test {
public static void main(String araguy[]) throws SQLException
{
String a,b,na,nu,np = null,cu,cp,da ;
Scanner scn = new Scanner(System.in);
DBUtil dbu = new DBUtil();
System.out.println("Enter Username : ");
a = scn.nextLine();
System.out.println("Enter password : ");
b = scn.nextLine();
/*-----------------------------------------------------------------------------*/
ResultSet rs = dbu.retrieve("Select * from info where name='"+ a +"' and address='"+b+"';");
String f = rs.getString("name");
String e = rs.getString("address");
if (a.equals(f)& b.equals(e))
{
System.out.println("Hello " + a + " Welcome to your account");
System.out.println( " What would be your next action ?");
System.out.println( "[cu] Change username " );
System.out.println( "[cp] Change password " );
System.out.println( "[da] Delete account ");
cu = scn.nextLine();
switch ( cu ){
case "cu":
System.out.println( "Enter new username" );
nu = scn.nextLine();
dbu.alter("update info set name ='"+nu+"' where name ='"+a+"';");
break;
case "cp":
System.out.println( "Enter new password" );
np = scn.nextLine();
dbu.alter("update info set name ='"+np+"' where name ='"+b+"';");
break;
case "da":
dbu.delete("delete from info where name='"+a+"';");
break;
}
}
else
{ System.out.println("NO Records of your account ! : would you like an" + "account ?");
System.out.println( "[y] Yes " );
System.out.println( "[n] No" );
na = scn.nextLine();
switch ( na ){
case "y":
System.out.println( "Enter username" );
nu = scn.nextLine();
System.out.println("Enter password");
np = scn.nextLine();
dbu.insert("insert into info values('"+ nu +"','"+np+"');");
case "n":
break;
}
/*----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------*/
dbu.disconnect();
}
}
}
-------------------------------------- my subclass ( with the connectors and stuff ) ---------------------------
package DBAccess;
import java.sql.*;
/**
*
* @author christian modified by moraxus
*/
public class DBUtil {
/*
* 0. load the driver
* 1. connect to the database
* 2. define the database to use
* 3. manipulate the tables
*/
Connection con;
Statement stmt;
PreparedStatement ob = null;
public DBUtil()
{
// 1. load the sql driver
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException ex) {
System.err.println(ex.getMessage());
}
String url = "jdbc:mysql://127.0.0.1/friends";
String userid = "root";
String pass = "root";
try
{
con = DriverManager.getConnection(url, userid, pass);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
}
}
/*-------------------------------------------------------------------------------------*/
public ResultSet retrieve(String commandStr)
{
try
{
ResultSet rs = stmt.executeQuery(commandStr);
return rs;
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
return null;
}
}
public int insert(String commandStr)
{
try
{
return stmt.executeUpdate(commandStr);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
return 0;
}
}
public int alter(String commandStr)
{
try
{
return stmt.executeUpdate(commandStr);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
return 0;
}
}
public int delete(String commandStr)
{
try
{
return stmt.executeUpdate(commandStr);
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
return 0;
}
}
public void disconnect()
{
try
{
stmt.close();
con.close();
}
catch( SQLException ex)
{
System.err.println(ex.getMessage());
}
}
}