Saturday, April 3, 2010

JAVA to MySQL Connection(ODBC)

Here we are going to discuss how to connect JAVA to MySQL
For portability and general purposes, we will be using the Open Database Connectivity(ODBC). Connecting to MySQL with the use of Open Database is similar to connecting to MS Access only with some additional steps.
Here's a copy of the database(dump file) I used in this tutorial. Just import it into your mysql. Download

Installing the Driver


1. Download MySQL ODBC Connector. I have a copy for windows users at www.mysqlodbccon.4shared.com . Please refer to the link below for more info.(Don't bother the filename if it says noinstall, you're still going to be installing it anyway)

2. Extract the Zip File and Run Install.bat (see, like I told you... although the batch (.bat) file copies the library(.dll) files into your operating system unlike real installers where they involve file extraction)


Install


3. A message will prompt that the instalation (copying of libraries) was successful.

Setting up the Data Source


1. We will now create a Data Source Name(DNS) which will be used as a reference by the program. First go to your control panel then Administrative Tools > Data Sources(ODBC)

control panel

2. The ODBC Data Source Dialog will then appear, select the System DSN tab.

Photobucket

3. Click on the Add button then another Dialog Box containing the Drivers will appear. Select MySQL ODBC Driver then click Finish.

odbc


4. Another Dialog Box will appear for the final configuration. Just fill up the neccessary inputs, the Data Source Name that you will be using on the text box. The Server should contain the local server name(you can use localhost or your computer name). Enter the user and password fields, and the database that you will be using as source.

GUI

5. When your done, click finish and you will notice that the data source has been added on the list of System Data Sources.

Coding


We will now explain the coding for connecting to the database
1. First import the neccessary libraries for Database connection, java.sql:

import java.sql.*;

2. Then instantiate your connection variables
Connection con;
Statement stmt;
ResultSet rs;

3. On your procedural method, create an exception handler
try{

//Your Code Goes Here
}catch(Exception e){
System.err.println(e);
}

4.In the try block, insert your connection code, First is the forced loading of the Database Driver in Class.forName() followed by the declaration of objects we instantiated earlier.
//Load the JdbcOdbc Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//Specify the Database URL where the DNS will be and the user and password
con = DriverManager.getConnection("jdbc:odbc:TOY2","root","root");

//Initialize the statement to be used, specify if rows are scrollable
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
//ResultSet will hold the data retrieved
rs = stmt.executeQuery("SELECT * FROM Humans");

//Display the results
while(rs.next()){
System.out.println(rs.getInt("ID") + " " + rs.getString("LastName") + " " + rs.getString("FirstName"));
}



For a full source code listing:
import java.sql.*;

public class ViewingMySQL {

public static void main(String[] args) {
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:TOY2","root","root");

stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT * FROM Humans");


while(rs.next()){
System.out.println(rs.getInt("ID") + " " + rs.getString("LastName") + " " + rs.getString("FirstName"));
}
}catch(Exception e){
System.err.println(e);
}
}
}



For more information about MySQL ODBC 5.1 Driver, including
installation instructions, please visit;

http://www.mysql.com/products/myodbc/index.html

MySQL and Java Developer's Guide

Friday, April 2, 2010

Programming Solutions by Language

"Rosetta Code is a programming chrestomathy site. The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different, and to aid a person with a grounding in one approach to a problem in learning another. Rosetta Code currently has 387 tasks, and covers 225 languages, though we do not (and cannot) have solutions to every task in every language.
A variety of tasks are listed, and visitors to this site are invited to solve the tasks in the language of their choice. The tasks cover everything from the mundane Empty Program to the classic Towers of Hanoi, the practical User Input, the mathematically-inclined Lucas-Lehmer test, and the involved yet entertaining RCRPG."


//SnailviN -- Based on one of its labels, this site is just for reference. Please do not rely on it too much because you will not experience the problems and will not learn it by yourself. =3 Hope this helps.

Sunday, March 28, 2010

Detailed Instructions on How to Download SharpDevelop and .NET Framework 3.5 SP1

This is the first time I do the posting here. The guy who is behind this blog (site specifically for all geeks like us) always asks me to post something useful like articles, tutorials and other things which can grasp your interests. Hope you like my first tutorial.

This is the present view (as of March 29, 2010) of the link I provided on his latest post.


IMAGE 1
To start using SharpDevelop, you need first to download it. Click LABEL 1 (refer to the image above) to download it to your computer. You will be transferred into their download page and save dialog box will appear.

IMAGE 2
Go to the directory you want this installer to save. Example, I want it to save in c drive.
SharpDevelop_3.2.0.5505_setup.msi will be saved in Local Drive (C:).

IMAGE 3
After you download it, .NET Framework is also needed in order your program to run. And that is .NET Framework 3.5 SP1 to be specific.
Follow these steps to have it:
1. Click LABEL 2 (refer to the first image above).
2. You will be transferred to this page:

IMAGE 4
3. Click Download
4. Microsoft download page will load upon doing step 3.

IMAGE 5
5. If the download does not start after 30 seconds, click the Start Download link.

IMAGE 6

Save dialog box will appear and asking you where to put it. After choosing the desired location, click save to download it.

6. And voila! You have it.

After you install all of these installers, (.NET Framework should be installed first before the SharpDevelop) you can start using it and develop your first program in whatever programming language you prefer. (either in vb, c# and others)


Hope this tutorial will be a help guys! Have a happy and enjoyable life. See you on my next post! ^^,

Thursday, March 18, 2010

SharpDevelop: Open Source Visual Studio .NET

Fot those who are looking for an alternative IDE for Microsoft Visual Studio .NET, something that costs less (actually free), and does not require a lot of hardware specification, this this article may be of help.

SharpDevelop is a free IDE that imitates the user interface of Microsoft Visual Studio. It contains a lot of features similar to Visual Studio such as the project explorer, drag-and-drop design view, resource manager, syntax highlighting... etc. It uses the free distributable .NET Framework for compiling source codes. (So it requires .NET Framework installed first)


 


SharpDevelop may not posses the same quality and features which may be found at Microsoft Visual Studio but this could also be a good option for starters especially in Visual Basic and C# Programming. Users who are already used to may not feel comfortable because of some features. However, projects in SharpDevelop may still be opened in MS Visual Studio.

sharpdevelop


SharpDevelop may be downloaded at
icsharpcode.net



Latest Version for Microsoft .NET Framework
.NET Framework Page

Tuesday, November 17, 2009

Using Continue

In Programming languages such as C/C++, JAVA, PHP, Python.. etc the keyword continue is a jump statement like break and goto.
This is used to skip inside an iteration.
An example for this is the following for loop statement that prints all even numbers from 0 - 10.

for(int num = 0;num <= 10; num++){
if ((num % 2) > 0)
continue;
printf("%d ", num);
}


The code is written in C Language (actually it is more like an algotithm)
so even JAVA programmers can easily translate them.
Another example, for more sophisticated programming, continue may be also used for deleting entries in file manipulation.

Friday, November 13, 2009

Downloadable JAVA EBOOKS

Free JAVA Lectures (Slideshow).exe Free Java Lectures

How to Program in JAVA.rar JAVA How to Program - the password in this ebook is itspot.
This is a fully featured book where you can learn Java. The best book I know in that programming language.


Thinking in JAVA

I have a copy of this book and I find it easy to understand because of the detailed explanation.

The JAVA turoial A short course on the basics.chm This book helped me a lot on projects in school

squidoo.com/itebooks