Articles in the Java Codes Category
Java Codes »
This example shows how to connect to a database, in this case a MS SQL Server database
and read rows from a table.
It is assumed that the table has 3 columns and they all contains string-values (datatype char, varchar etc.).
To connect to another database type, just change the driver and url to match the specific type.
Note that you’ll have to change some of the values to match your own environment(database, table, userid and password),
and to make it work, you will have to download the jdbc driver from Microsoft and put it …
Java Codes »
This example shows the different steps needed to connect to a database.
In the example the Microsoft SQL Server is used, but the steps applies to all types of database engines.
The first thing you need to do is to consider what driver to use. There are different types of drivers
(the different types is not covered here) and the most efficient and widely used driver is a Type 4 driver.
A Type 4 driver is completely written in Java and is not dependent on any native code. Each database engine has its own …
Java Codes »
This example demonstrates how to use a transaction with JDBC.
The only thing really that differs from ordinary database operations is that you set a boolean value on the Connection object.
The Connection object has a method called setAutoCommit which sets a value of the object that decides whether to execute the query immediately or not.
Default that value is true, so in order to utilize a transaction you need to set it to false. In the example a new connection is created and finally also closed.
It is though more common to have …
Java Codes »
This code example shows how to connect to a database (in this case MS SQL Server) and call a stored procedure.
To call the stored procedure you create an instance of the class CallableStatement by calling the method prepareCall() on the Connection object.
In the first example below we assume that the name of the stored procedure is “generateID” and it takes one string argument, and returns an Id which depends on the argument (in the example it returns employeeId).
The value of the argument is set by calling the method setString() on …
Java Codes »
This code example shows how to create a zip file.
What we do is to create an input stream from the file we want to compress, and while we read from it we write the contents to an output stream.
This output stream is of type ZipOutputStream which takes an FileOutputStream as parameter.
Next we have to add a zip entry to the output stream before we start writing to it.
We will also clean up by closing the zip entry and both the input stream and output stream when we are done.
import java.io.FileInputStream;
import …
Java Codes »
To list the contents of a zip file you need to get the entries of the file since every file in a zip file is represented by an entry.
In this case we assume that the filename of the zip file is ‘testfile.zip’. By calling the entries method of the ZipFile object we get an Enumeration back that can be used to loop through the entries of the file.
Note that we have to cast each element in the Enumeration to a ZipEntry.
Normally some more extensive processing of the entries would probably …
Java Codes »
This example shows how to extract a zipfile containing one file (or entry).
The name of the zip file is ‘compressed.zip’ and we want to write the contents to a file called ‘extracted.txt’.
We start by opening an input stream to the compressed file and an output stream to the file where we want the content to be extracted.
After that we get the next entry of the zip file (and the only entry in this case) – test so it’s not null, and then start reading the contents from the input stream …
Java Codes »
This example shows how to convert from an ArrayList to a HashSet. Since most Collection objects have a constructor that allows for passing in another Collection object we are able to make these kinds of conversions easily.
In the example we create an ArrayList to which we add a few string items. Next we create a HashSet and provide the reference to the ArrayList as argument to the HashSet constructor.
Finally we just loop through the HashSet and print out the contents.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
*
* @author javabout.com
*/
public class Main {
/**
* Conversion …
Java Codes »
This example shows how to do a Queue to List conversion. Since both Queue and List are of type Collection we can create any of them using the constructor that takes another Collection object as argument.
Here we create a LinkedList and add items to it, then we create an ArrayList and pass the LinkedList as an argument. Finally we print out the contents of the newly created ArrayList.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
*
* @author javabout.com
*/
public class Main {
/**
* Converts a Queue to a List, or if you will,
* converts a LinkedList …
Java Codes »
This example shows how to iterate through an object which implements the Collection interface. Here we use and ArrayList but the same code can roughly be applied to other classes that implements the Collection interface since they’ll also implement the Iterable interface.
In the example code we use three different ways that do exactly the same thing, namely to loop through the elements of the list and print them out.
First we create the ArrayList using generics which means that we specify of what type the elements of the list should be. …
