Russell Havik's Web, where all things started :)
 
home | for fun | graphics | me | portfolio | links | CELTICS.COM | CELTICS SCHEDULES & RESULTS
home / for fun / my java / Database Manager
 
 
Below links are designed for those who doesn't have Flash Plugins installed :)
 
Articles | My Celtics | My Java | My Prayers | Time to Brag | My win32 Applications
 

My JAVA - Database Manager
Please note that coding were created by me, if you want to use them, please state a little credit for me :-)
Most important. please use them for learning purpose only. No cheating and copying code for cut and paste, lots of practice will help to improve your knowledge...remember, no lemons no melons...

Please note: after you copy the codes below, save the file as file_name.JAVA and make sure the CLASS name is EXACTLY the same as the filename that you saved before...
Example: MyFirstJava.JAVA---contains class MyFirstJava{......}; easy peasy...:-)

The 'tab rules' suck when it comes to html, so don't be shock to see evry codes are on the same left side..sowie...

 

DatabaseWriter: Captures data entered, and saved it into a file (MyDataBase.dat)
import java.io.*;


class DataBaseWriter
{
public static void main (String S[])
{
String Name, Address;
int Age;
double Salary;
char Sex;
char Loop = 'Y';


while (Loop == 'Y')
{
System.out.println("Please supply the following data...");
System.out.print ("Your name >");
Name = KeyInput.ReadString();
System.out.print ("Your address >");
Address = KeyInput.ReadString();
System.out.print ("Your age >");
Age = KeyInput.ReadInt();
System.out.print ("Your salary >");
Salary = KeyInput.ReadDouble();
System.out.print ("Are you [M]ale or [F]emale? >");
Sex = KeyInput.ReadChar();

try
{
DataOutputStream DOS = new DataOutputStream (new FileOutputStream("MyDatabase.dat"));

DOS.writeInt(Age);
DOS.writeDouble(Salary);
DOS.writeChar(Sex);
DOS.writeInt(Name.length());
DOS.writeBytes(Name);
DOS.writeInt(Address.length());
DOS.writeBytes(Address);
System.out.println("The data was successfully stored to disk");
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
System.out.println("Enter more data? [Y] / [N]");
Loop = KeyInput.ReadChar();
if (Loop == 'N')
{
break;
}
}


}
};

abstract class KeyInput
{
private static String Fetch()
{
BufferedReader In = new BufferedReader(new InputStreamReader(System.in));

String Temp;
try
{
Temp = In.readLine();
}
catch (IOException e)
{
Temp = "";
}
return Temp;
}

public static String ReadString()
{
return Fetch();
}

public static int ReadInt()
{
String Line = Fetch();
int iVal = 0;
try
{
iVal = Integer.parseInt(Line);
}
catch(NumberFormatException e)
{
}
return iVal;
}

public static double ReadDouble()
{
String Line = Fetch();
double dVal = 0.0;
try
{
dVal = Double.parseDouble(Line);
}
catch(NumberFormatException e)
{
}
return dVal;
}


static char ReadChar()
{
return Fetch().charAt(0);
}
};


DatabaseReader: Read the data that has been saved into a file (MyDataBase.dat)
import java.io.*;


class DataBaseReader
{
public static void main (String S[])
{
String Name = null, Address = null;
int Age = 0;
double Salary = 0.0;
char Sex = 0;


try
{
DataInputStream DIS = new DataInputStream (new FileInputStream("MyDatabase.dat"));

Age = DIS.readInt();
Salary = DIS.readDouble();
Sex = DIS.readChar();

int Size = DIS.readInt(); // use Size
byte b[] = new byte[Size];
DIS.readFully(b, 0,Size) ;
Name = new String(b);

int length = DIS.readInt(); // use length from Karl's ori
b = new byte[length];
DIS.readFully(b, 0,length) ;
Address = new String(b);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}

System.out.print ("\nOkay, the data read was ....\n\n");
System.out.println("**********************************************************");
System.out.print ("Your name is " + Name + " and you live at " + Address + ".");
System.out.print ("\nYou earn " + Salary + " per year which is not bad for a " + Age);
System.out.println (" year old " + ((Sex == 'F') ? ("female.") : ("male.")));
System.out.println("**********************************************************");
}
};

 

 
 
+------------+
ICQ-93893008
[View] / [Sign]
G-Book 1
[View] / [Sign]
G-Book 2
 
ChangingLINKS
owned by russ
 
 
all.at/russellhavik
I got it from
v3.com
 
 
hosted for free by Geocities.com
 
link, link, link
 
russ' blog
 
css 2 official!!
home | for fun | graphics | me | portfolio | links © Celticslopedia - Russell Havik, 1998 - 2005
 
my blogs
Hosted by www.Geocities.ws

1