/**
* Opens a Microsoft Access Database without having need to
* have access rights to the Administrative Tools on Windows
* to set up any ODBC connections.
*/
import java.sql.*;
import java.io.*;
/**
* @author Ewald Horn
* @company JavaK
*/
public class ConnectDB
{
public static Connection con;
public static final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
public static final String url = "jdbc:odbc:" +
"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=";
String path; // where the database can be found
////////////////////////////////////////////////////////////
/**
* Sets the path to the database.
*/
public ConnectDB ()
{
path = "c:" + File.separator + "projects" +
File.separator + "tutorial.mdb";
}
////////////////////////////////////////////////////////////
/**
* Runner method for the TestAccess class.
*/
public void go ()
{
makeConnection ();
addRecipe ();
removeRecipe ();
updateRecipe ();
showRecipes ();
closeConnection ();
}
////////////////////////////////////////////////////////////
/**
* Creates the database connection.
*/
private void makeConnection ()
{
System.out.println ("Opening database...\n");
try
{
Class.forName (driver);
con = DriverManager.getConnection (url + path);
}
catch (Exception ex)
{
System.out.println ("Error opening the database!");
System.out.println (ex);
System.exit (0);
}
System.out.println ("Success!");
}
////////////////////////////////////////////////////////////
/**
* Removes a recipe from the database.
*/
private void removeRecipe ()
{
String sql = "DELETE FROM RECIPESTABLE WHERE RECIPEID=11";
System.out.print ("\nRemoving a recipe : ");
try
{
Statement statement = con.createStatement ();
int result = statement.executeUpdate (sql);
System.out.println (" Removed " + result + " recipe(s).");
}
catch (Exception ex)
{
System.out.println ("Error removing a recipe!");
System.out.println (ex);
}
}
////////////////////////////////////////////////////////////
/**
* Modifies an existing record.
*/
private void updateRecipe ()
{
String sql = "UPDATE RECIPESTABLE SET " +
"RECIPENAME='Pizza' WHERE RECIPEID=8";
System.out.print ("Updating a record : ");
try
{
Statement statement = con.createStatement ();
int result = statement.executeUpdate (sql);
System.out.println (" Updated " + result + " recipe(s).");
}
catch (Exception ex)
{
System.out.println ("Error removing a recipe!");
System.out.println (ex);
}
}
////////////////////////////////////////////////////////////
/**
* Adds a recipe to the database.
*/
private void addRecipe ()
{
String sql = "INSERT INTO RECIPESTABLE(RECIPENAME,INGREDIENTS)" +
" VALUES('Any Recipe','Ingredients')";
System.out.print ("\nAdding a recipe : ");
try
{
Statement statement = con.createStatement ();
int result = statement.executeUpdate (sql);
System.out.println (" Added " + result + " recipe(s).");
}
catch (Exception ex)
{
System.out.println ("Error adding a recipe!");
System.out.println (ex);
}
}
////////////////////////////////////////////////////////////
/**
* Displays all the recipes in the database.
*/
private void showRecipes ()
{
String sql = "SELECT * FROM RECIPESTABLE";
System.out.println ("\nRecipes in the database : \n");
try
{
Statement statement = con.createStatement ();
ResultSet rs = statement.executeQuery (sql);
if (rs != null)
{
while (rs.next ())
{
int recipeID = rs.getInt ("RECIPEID");
String recipeName = rs.getString ("RECIPENAME");
System.out.println (recipeID + " " + recipeName);
}
}
rs.close ();
statement.close ();
}
catch (Exception ex)
{
System.out.println ("Error reading database information!");
System.out.println (ex);
}
}
////////////////////////////////////////////////////////////
/**
* Closes the connection cleanly.
*/
private void closeConnection ()
{
System.out.println ("\nClosing database.");
try
{
con.close ();
}
catch (Exception ex)
{
System.out.println ("Error closing the database!");
System.out.println (ex);
}
}
////////////////////////////////////////////////////////////
/**
* Main method for ConnectDB.java
*/
public static void main (String args[])
{
ConnectDB testApp = new ConnectDB ();
testApp.go ();
}
}
Thursday, April 19, 2012
There's a PDF going around that I wrote a few years ago with a Database tutorial in it for Java. I've been asked to please release the source code, so, here it is. This code will connect to a Microsoft Access database file on the local system and allow you to read and write via JDBC.
Wednesday, March 7, 2012
JavaK has been updated with a new Java GUI primer. If you've never done any Java Swing development, this short tutorial will take you through the steps and have you building GUI's in no time.
Wednesday, February 29, 2012
A student asked me if there's a simple way of making a method that takes a variable number of arguments. In Java, using the ... notation, you can do just that!
Let's say, for example, you want to create a method that will add up any number of integer numbers. Using the code below, you can do just that. So simple, so easy, so elegant.
public class AddUpMany
{ public int addThemAll(int... numbers) { int total = 0; for (int temp : numbers)
{ total += temp; } return total; } public static void main(String args[])
{ AddUpMany instance = new AddUpMany(); System.out.println("Adding up 5 and 6 gives you " + instance.addThemAll(5,6)); System.out.println("Adding up 5,5 and 2 gives you " + instance.addThemAll(5,5,2)); System.out.println("Adding up 1,2 and 3 gives you " + instance.addThemAll(1,2,3)); } }
Saturday, February 25, 2012
Javascript Lessons
It's so often these days that I get asked questions around Javascript, that I felt I should find a resource for my students. After digging around a bit, I found Code Academy, a brilliant site that offers a free Javascript course.
Why Javascript if this site is about Java? Well, some students have started to write all sorts of interesting applications as projects, especially in the mobile space. With smart-phones all the rage these days, and, admit it, some of these devices are incredible, more and more students are interested in developing for them. By using Java as the back-end, they write server applications with a HTML / Javascript interface.
If you are interesting in learning Javascript for free, I'd recommend you give the Code Academy guys a try, it's a great site, with really easy-to-follow lessons that will get you up to speed in just a couple of hours.
Why Javascript if this site is about Java? Well, some students have started to write all sorts of interesting applications as projects, especially in the mobile space. With smart-phones all the rage these days, and, admit it, some of these devices are incredible, more and more students are interested in developing for them. By using Java as the back-end, they write server applications with a HTML / Javascript interface.
If you are interesting in learning Javascript for free, I'd recommend you give the Code Academy guys a try, it's a great site, with really easy-to-follow lessons that will get you up to speed in just a couple of hours.
Monday, September 6, 2010
Another one in Stellenbosch - Umonya
It's happening again!
There's ANOTHER free Python course being held by Umonya at the University of Stellenbosch. Students will be given the opportunity to learn the Python programming language, interact with industry players and, most importantly, learn that computers are not all that scary.
Please visit Umonya for more information regarding this fantastic outreach project.
There's ANOTHER free Python course being held by Umonya at the University of Stellenbosch. Students will be given the opportunity to learn the Python programming language, interact with industry players and, most importantly, learn that computers are not all that scary.
Please visit Umonya for more information regarding this fantastic outreach project.
Tuesday, August 31, 2010
Great stuff in Cape Town
The guys at Umonya are doing fantastic things for school kids in South Africa. They offer free Python courses over selected weekends where children with no previous programming experience are taught how to program in Python.
Not only do they show these kids things they might have thought impossible before, but they also make time to have chats with the kids to open their eyes to career possibilities that many of them have never thought off.
The courses are organized by UCT students, but, in my humble opinion, reflect the same level of quality, commitment and dedication one would expect from professional trainers. These guys need your help and support, so if your company (or yourself) can be involved in any way, do step up and help cultivate the next generation of IT professionals for Africa.
Not only do they show these kids things they might have thought impossible before, but they also make time to have chats with the kids to open their eyes to career possibilities that many of them have never thought off.
The courses are organized by UCT students, but, in my humble opinion, reflect the same level of quality, commitment and dedication one would expect from professional trainers. These guys need your help and support, so if your company (or yourself) can be involved in any way, do step up and help cultivate the next generation of IT professionals for Africa.
Friday, August 20, 2010
Telesure Cesspool
I know this is not blog related, but really, I've had it with these guys.
RANT ON
We had an accident in April 2010, and things are STILL not sorted out by these Telesure, Auto&General, AA Insurance incompetents. Did you know Dial Direct, Budget, Auto & General and First for Woman are ALL just brands of Telesure? And if you go on a site like Hippo, you are actually getting quotes from the same company, just under different brand names.
So here's a little bit of free advice - stay away from Telesure and their brands, it's just the most pathetic service I have ever received. In fact, I'm going to write a nice long article about it and hopefully save someone all the hassle I had to go through. It takes days for them to get back to you, even though they have a so-called service charter that they clearly do not stick to.
Do proper research when you are considering your insurance provider and make sure they are underwritten by a reputable company. I made the mistake of trusting in the AA brand name, AA Insurance is useless, they are simply brokers and did not lift a finger to try to assist us.
RANT OFF
RANT ON
We had an accident in April 2010, and things are STILL not sorted out by these Telesure, Auto&General, AA Insurance incompetents. Did you know Dial Direct, Budget, Auto & General and First for Woman are ALL just brands of Telesure? And if you go on a site like Hippo, you are actually getting quotes from the same company, just under different brand names.
So here's a little bit of free advice - stay away from Telesure and their brands, it's just the most pathetic service I have ever received. In fact, I'm going to write a nice long article about it and hopefully save someone all the hassle I had to go through. It takes days for them to get back to you, even though they have a so-called service charter that they clearly do not stick to.
Do proper research when you are considering your insurance provider and make sure they are underwritten by a reputable company. I made the mistake of trusting in the AA brand name, AA Insurance is useless, they are simply brokers and did not lift a finger to try to assist us.
RANT OFF
Subscribe to:
Posts (Atom)