Feeds:
Posts
Comments

Archive for the ‘Java’ Category

Hi;

It’s been too long time to wrote article. Whatever …

I try to increased float data type in loop at php and java. But it didn’t give me a true result at last 2 or 3 step.

for example try this at java;

		for (float i=0f; i<=1f; i+=0.1){
			System.out.println(i);
		}

You’ll see this

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.70000005
0.8000001
0.9000001

The solution is;

		for (int count = 0; count <= 10; count += 1) {
			float x = count / 10.0f;
			System.out.println(x);
		}

This shows you the true results;

0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

See you on other articles…

Advertisements

Read Full Post »

Hello Everyone;

Now we are going to fill the JTable with sql query in java. We have to use swing for use of JTable, because Jtable, JScrollPane and JFrame use the Swing library. We use JFrame for create a window, JScrollPane for scroll the data and JTable for list data. We’ll fill JTable with AbstractTableModel and create two class. The 1st. class shows data in the window and the 2nd. class creates table model for fills the JTable. I selected oracle’s all_tables view for listed data. You can see the codes at below;

import java.sql.*;
import java.util.*;
import javax.swing.table.*;

public class FillTable extends AbstractTableModel{

 /**
 *
 */
 private static final long serialVersionUID = -912060609250881296L;
 private ResultSet rs;
 private int rowCount;
 private int columnCount;
 private ArrayList data=new ArrayList();

 public FillTable(ResultSet _rs) throws Exception
 {
 setRS(_rs);
 }

 public void setRS(ResultSet _rs)
 throws Exception
 {
 this.rs=_rs;
 ResultSetMetaData metaData=_rs.getMetaData();
 rowCount=0;
 columnCount=metaData.getColumnCount();
 while(_rs.next()){
 Object[] row=new Object[columnCount];
 for(int j=0;j<columnCount;j++){
 row[j]=_rs.getObject(j+1);
 }
 data.add(row);
 rowCount++;
 }
 }

 public int getColumnCount(){
 return columnCount;
 }

 public int getRowCount(){
 return rowCount;
 }

 public Object getValueAt(int rowIndex, int columnIndex){
 Object[] row=(Object[]) data.get(rowIndex);
 return row[columnIndex];
 }

 public String getColumnName(int columnIndex){
 try{
 ResultSetMetaData metaData=rs.getMetaData();
 return metaData.getColumnName(columnIndex+1);
 }catch(Exception e){
 e.printStackTrace();
 return null;
 }
 }
}

This class creates table model for fills JTable. Before we fill the JTable,  we have to fill the AbstractTableModel, so we talk about MVC in here. (Model View Controller) Let’s see how to  use this class.

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class MyTable {
 public static void main(String[] argv) throws Exception {
 JTable table = new JTable(myModel());

 JFrame f = new JFrame();
 f.setSize(300, 300);
 f.add(new JScrollPane(table));
 WindowListener wClose = new WindowAdapter() {
 public void windowClosing(WindowEvent e){
 System.exit(0);
 }
 };
 f.setVisible(true);

 }

 public static FillTable myModel() throws Exception{
 try {
 Class.forName ("oracle.jdbc.driver.OracleDriver");
 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 }

 Connection con = DriverManager.getConnection
("jdbc:oracle:thin:@Host:port:sid", "user", "pass");

 Statement st = con.createStatement();
 ResultSet rs = st.executeQuery("select owner,table_name,tablespace_name from all_tables");
 FillTable model = new FillTable(rs);
 return model;
 }
}

In this class we are getting a results from sql query and we are creating an object with this results. And then we are creating JTable with this object. The another usage is setModel method.You can use this method as “table.setModel(myModel());”. When you are running the program, you are going to see the data in Jtable.

Read Full Post »

Writing xlsx with java…

Hello again;

Today i want to tell you, how do you create and write excel files in java. First of all we must download apache’s POI api in here. (The JAVA api for Microsoft Document.) In fact this api doesn’t for only excel. You can use this api for word, power point, and etc… It supports office from 97 to 2007. You can create,read or write xls, xlsx, doc, docx and etc.. I must say this api is more fastest than c# office interop api. You must add jars into your project.

Now we create an excel xlsx file and we write some text in this file.  Ok lets write some code.

package excel;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class exelim {

 /**
 * @param args
 * @throws IOException
 */
 public static void main(String[] args) throws IOException {
 // TODO Auto-generated method stub
 Workbook wb = new XSSFWorkbook();
 FileOutputStream fos = new FileOutputStream("sample.xlsx");
 Sheet sh = wb.createSheet("new sheet 1");
 for (int k = 0; k < 30; k++) {
 Row row = sh.createRow((short)k);
 for (int i = 0; i < 30; i++) {
 Cell cell = row.createCell((short)i);
 cell.setCellValue("hi world : )");
 }
 }
 wb.write(fos);
 fos.close();
 }

}

You must create workbook, sheet, row and cell for write something into the cell. I use XSSF for xlsx, if you want to create, read or write xls you can use HSSF. For about more information please visit POI project website. You can find more good artice at there.

See you at another article.

Read Full Post »

Hi;

In this article i explain how to connect oracle with jdbc.  Before we start, we must download oracle oci driver for oracle version. You can find these oci drivers from here.

I use Oracle driver for Oracle 10.x database. After you download the Oracle oci driver we can start . We add new Java Project in eclipse. In libraries from “add external jars”, you must choice oci driver jar file.(I select ojdbc14.jar for oracle 10.x.) We add a main class for getting output from sql.

import java.sql.*;

public class main {

 /**
 * @param args
 * @throws SQLException
 */
 public static void main(String[] args) throws SQLException {
 // TODO Auto-generated method stub
 try {
 Class.forName ("oracle.jdbc.driver.OracleDriver");
 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 }

 Connection con = DriverManager.getConnection
 ("jdbc:oracle:thin:@ngeen-Laptop:1521:xe", "ngeen", "202020");
// @machine:port:SID,   user,  pass

 Statement st = con.createStatement();
 ResultSet rs = st.executeQuery("select * from all_tables");
 while (rs.next())
System.out.println (rs.getString(1));   // echo column 1
 st.close();
 }
}

If you look at the console you can see 1st column of all_tables view in database.

Read Full Post »

Change It Easy v1.0

Hi;

I wrote an open source program in java. And i called it Change It Easy. This program have easy function for now. It lists a running proxy from the proxy list website. You can download this project and source from here. It runs in linux, windows and etc… For execute this program you have to install JRE(java runtime environment).

At future;
> It change your ip with proxy in running proxy list.
> Language Pack.
> And etc..

I will waiting your help. Please contact with me for your ideas.

Read Full Post »