Setting up internet access through proxy server
Today I had a trouble in my workplace when a client application, running through a terminal (DOS prompt), not reach the server hosted in the internet, this is because the company where I work use proxy.
After some tries I found two solutions to solve my access problem: first was pass as Java parameters the proxy's configurations.
$ java -Dhttp.proxyHost=serv -Dhttp.proxyPort=port -Dhttp.proxyUser=user -Dhttp.proxyPassword=pass ClassJava
-Dhttp.proxyHost = IP or host name of server proxy
-Dhttp.proxyPort = Proxy port
-Dhttp.proxyUser = User
-Dhttp.proxyPassword = Password
This way the class ClassJava will have access to internet through proxy server.
Another way, was insert the settings in own class:
public static void main(String[] args){
...
System.getProperties().put("proxySet", "true");
System.getProperties().put("http.proxyHost", "serv");
System.getProperties().put("http.proxyPort", "port");
System.getProperties().put("http.proxyUser", "user");
System.getProperties().put("http.proxyPassword", "password");
...
}
proxySet = Connect, or not, through proxy server
http.proxyHost = IP or host name of server proxy
http.proxyPortt = Proxy port
http.proxyUse = User
http.proxyPassword = Password
Using this resource you can set your application to access the internet through proxy server.
I hope help you. Bye!
Creating transparent and shaped windows using Java Swing/AWT
The Java AWT offer various possibilities about window manipulation. Today I'll talk about two possibilities using class AWTUtilities.
Building a transparent window
To make a Window (JFrame, JDialog ...) with alpha effect, you should invoke the method AWTUtilities.setWindowOpacity. This method parameters are: the window to apply transparency and transparency degree, that can be between 0 (zero) and 1, being 0 invisible and 1 totally visible.
JFrame window = new JFrame("My Window");
//70% of transparency
AWTUtilities.setWindowOpacity(window, .7f);
window.setSize(800,600);
window.setVisible(true);
Changing window shape
To change the window shape you should use the method AWTUtilities.setWindowShape. This method parameters are: window that will be changed and the new shape (java.awt.Shape) of the window.
The most efficiently form to use is to implement the method componentResized(), cause you may recompute the window and components size
Here I'll use a triangle with 70% of transparency.
final JFrame window = new JFrame("My Window");
try {
//Add the ComponentListener to implement componentResized method
window.addComponentListener(new ComponentAdapter(){
@Override
//building componentResized
public void componentResized(ComponentEvent e) {
int[] x = {0,400,800}; //Pontos X do polígono
int[] y = {600,0,600}; //Pontos Y do polígono
//Triangle with 800 w x 600 h
Shape shape = new Polygon(x, y, 3);
AWTUtilities.setWindowShape(window, shape);
//70% of transparency
AWTUtilities.setWindowOpacity(window, 0.7f);
}
});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
window.setUndecorated(true); //removing title bar
window.setSize(800,600);
window.setVisible(true);
Notice that the method setUndecorated( ) was invoked with true as value. This method are responsible by hide the title bar (one with icon and the maximize, minimize and close buttons). This is really necessary, because with title bar visible the window are not be shaped.
I hope you enjoy...
Saving files in BLOB table column of a data base
To insert a file, is it in any format, you need call the method setBinaryStream, implemented by PreparedStatement.
PreparedStatemente.setBinaryStream(int index, Inputstream is, int length);
In sample, we set a table called FILEthat contains BLOB column called BIN.
//Normal connection, as any JDBC connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@<IP>:<PORT>:<SID>","<USER>","<PASSWORD>");
//Reading the file and retrieving an InputStream
File file= new File("<COMPLETE_FILE_PATH>");
FileInputStream fis = new FileInputStream(file);
//Preparing statement
PreparedStatement ps = conn.prepareStatement("INSERT INTO FILE(bin) VALUES(?)");
//Passing InputStream and file length
ps.setBinaryStream(1, fis, (int)file.length());
ps.execute();
ps.close();
conn.close();
I used Oracle 8i to execute this sample. I haven't a MySQL/PostgreSQL/MS SQL Server in my dispose, then you'll responsible for testing in this data bases and send me the results, OK
Thanks! Until next time!
Retrieving an Oracle cursor in Java
Many people come here looking for one way to retrieve cursors of Oracle procedures in Java. To them, I've here are a tutorial showing how to do this.
To retrieve the cursor you should declare him how a REF CURSOR in Package spec.
--Creating the REF CURSOR type type g_cursor is ref cursor;
In both, spec and body, you need declare an out REF CURSOR variable in procedure signature, how cited above.
procedure PRO_RETURN_CARS(
i_id in tbl_car.car_id%type,
o_cursor in out g_cursor);
The cursor must be opened in procedure's body to return, this way:
open o_cursor for
select car_id, company, model, color, hp, price
from tbl_car
where car_id = i_id;
The complete Package:
create or replace package PAC_CURSOR is
--Creating REF CURSOR type
type g_cursor is ref cursor;
--Procedure that return the cursor
procedure PRO_RETURN_CARS(
i_id in tbl_car.car_id%type,
o_cursor in out g_cursor); -- Our cursor
end PAC_CURSOR;
/
create or replace package body PAC_CURSOR is
procedure PRO_RETURN_CARS(
i_id in tbl_car.car_id%type,
o_cursor in out g_cursor) is
begin
--Opening the cursor to return matched rows
open o_cursor for
select car_id, company, model, color, hp, price
from tbl_car
where car_id = i_id;
end PRO_RETURN_CARS;
end PAC_CURSOR;
We have Oracle side ready, now we need create Java call
How the cursors are being returned by a procedure, we'll used a java.sql.CallableStatement instance.
CallableStatement cs = conn.prepareCall("{call PAC_CURSOR.PRO_RETURN_CARS(?,?)}");
The registerOutParameter will obtain oracle.jdbc.OracleTypes.CURSOR type and return a java.sql.ResultSet instance. We can iterate the ResultSet like a common Iterator.
Each row column returned by SELECT will be represented how a map, using correspondent getter. For example, we will call getString(<column name>) method when value of column is a varchar, getDate(<column name>) when is a date and etc.
The complete code will be like this:
//Calling Oracle procedure
CallableStatement cs = conn.prepareCall("{call PAC_CURSOR.PRO_RETURN_CARS(?,?)}");
//Defining type of return
cs.registerOutParameter("o_cursor", OracleTypes.CURSOR);
cs.setLong("i_id", id);
cs.execute();//Running the call
//Retrieving the cursor as ResultSet
ResultSet rs = (ResultSet)cs.getObject("o_cursor");
//Iterating the returned rows
while(rs.next()){
//Getting column values
System.out.println("ID: " + rs.getLong("car_id"));
System.out.println("Manufacturer: " + rs.getString("company"));
System.out.println("Model: " + rs.getString("model"));
System.out.println("Color: " + rs.getString("color"));
System.out.println("HP: " + rs.getString("hp"));
System.out.println("Price: " + rs.getFloat("price"));
}
In the end you will get any value returned in a SELECT clause.
See ya!
Generating ‘EXE’ to start your Java applications
Many developers need, or have needed, distribute their Java applications so that Windows users could start them naturally, not running java -jar <jar file> command or batch (.BAT) file.
I've been there too, then I found a easy and with many resources solution: JSmooth
This software allow you "transform" your JAR file in an executable (EXE), but, of course, will need the JVM already installed and running.
Here I only have mentioned the settings that I consider important, then let's go!
Download JSmooth in http://sourceforge.net/projects/jsmooth/files/;
After install (or unzip, this depends of file that you have downloaded) run him;
In the side menu, click in "Skeleton";
In the "Skeleton Selection" screen you need define how application will be run, select "Window Wrapper".
In "Skeleton Properties", you need define a message when user have no JVM installed ("Message" field) and where can be downloaded ("URL" field).
The "Launch java app in the exe process" field define if JAR file will be executed in same process of EXE (only the executable process will be displayed in Windows Task Manager), otherwise the javaw.exe will be displayed too.
The "Single Instance" field define if more than one instance can be opened.
"Debug Console" open the EXE from prompt window, displaying possibles outputs.
Now click in "Executable"
In "Executable Setting" you inform where EXE will be builded ("Executable Binary" field), the EXE's icon ("Executable Icon" filed) and what will be the application execution directory.
Click in "Application"
First, click in the icon
and select JAR that contains the main class.
After, select the main class in the field "Main Class" clicking in button
.
In the field "Application arguments" you can inform necessaries parameters for your main class.
"Embedded JAR" field allows you to aggregate your JAR file in EXE file, in other words, only EXE file will be necessarie, because the JAR will be uncompressed by EXE in each execution.
Now click in "JVM Selection".
Here you can define the minimum and maximum version of JVM that your application support.
The "JVM Serach Sequence" inform the seek order of javaw.exe file, in this case, he search in resgistry first, after in JAVA_HOME enviroment and so.
Click in "JVM Configuration".
Here the maximum and minimum memory available for your application can be configured, as the options that can passed to JVM.
Until here we only have configured the JSmooth. To build the EXE file click in button
. If you don't have saved the project, a new window will open to choose the place to save. Done it, the EXE file will be create in directory mentioned in "Executable Binary" field of "Executable" screen.
Now just execute the EXE file and your application will be run!
To more information visit http://jsmooth.sourceforge.net/
I hope you enjoyed, feel free to comment!
Until next!






