ResultSet res = stat.executeQuery();
res.last();
int n = res.getRow(); // no. of rows in result
29 Şubat 2012 Çarşamba
28 Şubat 2012 Salı
Java - TCP File Transfer
**********CLIENT*********
public static void main(String[] args) {
byte[] aByte = new byte[1];
int bytesRead;
Socket clientSocket = null;
InputStream is = null;
try {
clientSocket = new Socket("127.0.0.1", 3248);
is = clientSocket.getInputStream();
} catch (IOException ex) {
// Do exception handling
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (is != null) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("out.txt");
bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
clientSocket.close();
} catch (IOException ex) {
// Do exception handling
}
}
}
*********SERVER**************
public static void main(String[] args) {
while (true) {
ServerSocket welcomeSocket = null;
Socket connectionSocket = null;
BufferedOutputStream outToClient = null;
try {
welcomeSocket = new ServerSocket(3248);
connectionSocket = welcomeSocket.accept();
outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
} catch (IOException ex) {
// Do exception handling
}
if (outToClient != null) {
File myFile = new File("in.txt");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException ex) {
// Do exception handling
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(mybytearray, 0, mybytearray.length);
outToClient.write(mybytearray, 0, mybytearray.length);
outToClient.flush();
outToClient.close();
connectionSocket.close();
// File sent, exit the main method
return;
} catch (IOException ex) {
// Do exception handling
}
}
}
}
public static void main(String[] args) {
byte[] aByte = new byte[1];
int bytesRead;
Socket clientSocket = null;
InputStream is = null;
try {
clientSocket = new Socket("127.0.0.1", 3248);
is = clientSocket.getInputStream();
} catch (IOException ex) {
// Do exception handling
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (is != null) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream("out.txt");
bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
clientSocket.close();
} catch (IOException ex) {
// Do exception handling
}
}
}
*********SERVER**************
public static void main(String[] args) {
while (true) {
ServerSocket welcomeSocket = null;
Socket connectionSocket = null;
BufferedOutputStream outToClient = null;
try {
welcomeSocket = new ServerSocket(3248);
connectionSocket = welcomeSocket.accept();
outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
} catch (IOException ex) {
// Do exception handling
}
if (outToClient != null) {
File myFile = new File("in.txt");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException ex) {
// Do exception handling
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(mybytearray, 0, mybytearray.length);
outToClient.write(mybytearray, 0, mybytearray.length);
outToClient.flush();
outToClient.close();
connectionSocket.close();
// File sent, exit the main method
return;
} catch (IOException ex) {
// Do exception handling
}
}
}
}
Java TCP Client - Server
//***Client*******//
public static void main(String[] args) {
Socket socket;
InetAddress address;
DataOutputStream os;
try {
address = InetAddress.getByName("127.0.0.1");
socket = new Socket(address, 20000);
os = new DataOutputStream(socket.getOutputStream());
os.writeBytes("helllo");
os.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//***Server*******//
public static void main(String[] args) {
ServerSocket serverSocket;
InetAddress serverAddress;
DataInputStream is;
try {
serverAddress = InetAddress.getByName("127.0.0.1");
serverSocket = new ServerSocket(20000, 10, serverAddress);
Socket client = serverSocket.accept();
is = new DataInputStream(client.getInputStream());
String str = is.readLine();
System.out.print(str);
is.close();
client.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Socket socket;
InetAddress address;
DataOutputStream os;
try {
address = InetAddress.getByName("127.0.0.1");
socket = new Socket(address, 20000);
os = new DataOutputStream(socket.getOutputStream());
os.writeBytes("helllo");
os.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//***Server*******//
public static void main(String[] args) {
ServerSocket serverSocket;
InetAddress serverAddress;
DataInputStream is;
try {
serverAddress = InetAddress.getByName("127.0.0.1");
serverSocket = new ServerSocket(20000, 10, serverAddress);
Socket client = serverSocket.accept();
is = new DataInputStream(client.getInputStream());
String str = is.readLine();
System.out.print(str);
is.close();
client.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
27 Şubat 2012 Pazartesi
Java - Open folder or directory using java
import java.util.*;
import java.io.*;
import java.awt.Desktop;
class OpenFolderInJava{
public static void main(String[] arg){
String path = "."; // path to the directory to be opened
File file = new File("C:\\");
Desktop desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
try {
desktop.open(file);
}
catch (IOException e){
}
}
}
import java.io.*;
import java.awt.Desktop;
class OpenFolderInJava{
public static void main(String[] arg){
String path = "."; // path to the directory to be opened
File file = new File("C:\\");
Desktop desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
try {
desktop.open(file);
}
catch (IOException e){
}
}
}
25 Şubat 2012 Cumartesi
JavaMail – GMail via SSL
To send an email using your Java Application is simple enough but to start with you should have JavaMail API and Java Activation Framework (JAF) installed on your machine.
You can download latest version of JavaMail from Java's standard website.
You can download latest version of JAF from Java's standard website.
package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailSSL {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@no-spam.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@no-spam.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
You can download latest version of JavaMail from Java's standard website.
You can download latest version of JAF from Java's standard website.
package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailSSL {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@no-spam.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to@no-spam.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
22 Şubat 2012 Çarşamba
Open existing project in Eclipse
Use File > Import and select General > Existing Projects into Workspace.
Click next and then browse to the directory contain the project directory.
Click next and then browse to the directory contain the project directory.
20 Şubat 2012 Pazartesi
Setting the JAVA_HOME Variable
Once you have identified the JRE installation path:
1. Right-click the My Computer icon on your desktop and select Properties.
2. Click the Advanced tab.
3. Click the Environment Variables button.
4. Under System Variables, click New.
5. Enter the variable name as JAVA_HOME.
6. Enter the variable value as the installation path for the Java Development Kit.
7. If your Java installation directory has a space in its path name, you should use the shortened path name
(e.g. C:\Progra~1\Java\jre6) in the environment variable instead.
8. Click OK.
9. Click Apply Changes.
10. Restart Windows. (This is not always necessary, but it often prevents problems.)
11. If you are running the Confluence EAR/WAR distribution, rather than the regular Confluence distribution, you may need to restart your application server.
1. Right-click the My Computer icon on your desktop and select Properties.
2. Click the Advanced tab.
3. Click the Environment Variables button.
4. Under System Variables, click New.
5. Enter the variable name as JAVA_HOME.
6. Enter the variable value as the installation path for the Java Development Kit.
7. If your Java installation directory has a space in its path name, you should use the shortened path name
(e.g. C:\Progra~1\Java\jre6) in the environment variable instead.
8. Click OK.
9. Click Apply Changes.
10. Restart Windows. (This is not always necessary, but it often prevents problems.)
11. If you are running the Confluence EAR/WAR distribution, rather than the regular Confluence distribution, you may need to restart your application server.