Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, 19 October 2010

How to start with SQLite?

How to start with SQLite?

SQLite is very easy to use, simple and very light. The database is saved in an only file.
SQLite works with MySQL syntax.

The easiest mode to create a database is using a client for SQLite. My recommendation is a plugin fo firefox called SQLite Manager.
You can get it here.

If you want connect your SQLite databse with Java, you will need the SQLite Connector:
You can get it here.
Below I explain the first steps to use it.






Initialize:


try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:"+DBpath);
}
catch(Exception e){
e.printStackTrace();
}

Execute query:

String select = "SELECT * FROM CONTENT WHERE ID=?";
PreparedStatement ps = conn.prepareStatement(select);
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
if(rs.getString("CONTENT")!=null){
String path = rs.getString("CONTENT");
}
ps.close();
rs.close();

Execute Update:

ResultSet rs = null;
PreparedStatement ps = null;

ps = conn.prepareStatement("UPDATE CONTENT SET SIZE=?, TYPE=?, TIME=? WHERE ID=?");
ps.setInt(1, Integer.valueOf(size));
ps.setString(2, type);
ps.setDate(3, date);
ps.setString(4, id);
ps.executeUpdate();

ps.close();

Thanks for read me!

Monday, 18 October 2010

How to use UUID?

How to use UUID?



















In java:


First, we have to import the library:

import java.util.UUID;

Finally, we generate a random UUID.

String newId = UUID.randomUUID().toString();

Easy? Yes, a lot.

Tuesday, 14 September 2010

How to use xls ( Excel ) files in Java

How to use xls (Excel) files in Java

First, you need the POI library. You can download it here.


To work with a xls File with Java is very easy. Simply, you have to fix in Rows and Cells.













I show you a part of source code, it's more easy to understand.

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

Workbook wb = new XSSFWorkbook();
FileOutputStream fos = new FileOutputStream("C:\\sample.xlsx");
Sheet sh = wb.createSheet("newSheet1");
sh.createRow(0);

sh.getRow(0).createCell(0).setCellValue("Hours");
sh.getRow(0).createCell(1).setCellValue("Sunday");
sh.getRow(0).createCell(2).setCellValue("Monday");
sh.getRow(0).createCell(3).setCellValue("Tuesday");
sh.getRow(0).createCell(4).setCellValue("Wednesday");
sh.getRow(0).createCell(5).setCellValue("Thursday");
sh.getRow(0).createCell(6).setCellValue("Friday");
sh.getRow(0).createCell(7).setCellValue("Saturday");





for(int k = 1; k<25;k++){
sh.createRow(k);
sh.getRow(k).createCell(0).setCellValue(k-1);
}

for (int i = 0; i <>
for(int j = 0; j<24;j++){
sh.getRow(j+1).createCell(i+1).setCellValue("Row "+j+" Cell "+i);
}
}

To finish correctly you need add this:

wb.write(fos);
fos.close();




















Now, the file already contents the data.

See you.

Friday, 3 September 2010

Java: Obtener codigo fuente de una pagina web

Java: Obtener código fuente de una página web

Bueno, pues aquí un sencillo código:

String url = "http://www.google.com";
try{
URL url = new URL(url);
URLConnection conn = url.openConnection();
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String data;
while ((data = in.readLine()) != null) {
//lo que tu quieras hacer con cada linea
}
}
catch (Exception ex){
ex.printStackTrace();
System.out.println("ERROR: "+ex.getMessage());
}
Un saludo.