blog.visualp.com
import java.io.*; public class Person implements Serializable { public String firstName; public String lastName; private String password; transient Thread worker; public Person(String firstName, String lastName, String password) { this.firstName = firstName; this.lastName = lastName; this.password = password; } public String toString() { return new String(lastName + ", " + firstName); } } class WritePerson { public static void main(String [] args) { Person p = new Person("Fred", "Wesley", "cantguessthis"); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("Person.ser")); oos.writeObject(p); } catch (Exception e) { e.printStackTrace(); } finally { if (oos != null) { try {oos.flush();} catch (IOException ioe) {} try {oos.close();} catch (IOException ioe) {} } } } } class ReadPerson { public static void main(String [] args) { ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("Person.ser")); Object o = ois.readObject(); System.out.println("Read object " + o); } catch (Exception e) { e.printStackTrace(); } finally { if (ois != null) { try {ois.close();} catch (IOException ioe) {} } } } }
Read object Wesley, Fred
public class Person implements Serializable { public String firstName; public String lastName; int age; private String password; transient Thread worker; public Person(String firstName, String lastName, String password, int age) { this.firstName = firstName; this.lastName = lastName; this.password = password; this.age = age; } public String toString() { return new String(lastName + ", " + firstName + " age " + age); } } class WritePerson { public static void main(String [] args) { Person p = new Person("Fred", "Wesley", "cantguessthis", 31); // 이후는 원본과 같습니다.
static final long serialVersionUID = /* some long integer */;
static final long serialVersionUID = 4070409649129120458L;
private void writeObject(ObjectOutputStream stream) throws IOException; private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException;
import java.io.*; public class Person implements Serializable { public String firstName; public String lastName; int age; private String password; transient Thread worker; static final long serialVersionUID = 4070409649129120458L; //이것은 제대로 된 기호화 연산이 아니지만, 실행은 됩니다. //하지만 조금 더 나은 것으로 대신해야 할 것입니다. static String crypt(String input, int offset) { StringBuffer sb = new StringBuffer(); for (int n=0; n<input.length(); n++) { sb.append((char)(offset+input.charAt(n))); } return sb.toString(); } // 실제 응용프로그램에서는 패스워드로의 액세스를 동조하도록 하면서 // 페스워드를 System.out 로 출력해서는 안됩니다! private void writeObject(ObjectOutputStream stream) throws IOException { password = crypt(password, 3); System.out.println("Password encyrpted as " + password); stream.defaultWriteObject(); password = crypt(password, -3); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); password = crypt(password, -3); System.out.println("Password decrypted to " + password); } public Person(String firstName, String lastName, String password, int age) { this.firstName = firstName; this.lastName = lastName; this.password = password; this.age = age; } public String toString() { return new String(lastName + ", " + firstName + " age " + age); } }
import java.io.*; public class Person implements Serializable { public String fullName; int age; private String password; transient Thread worker; static final long serialVersionUID = 4070409649129120458L; //이것은 제대로 된 기호화 연산이 아니지만, 실행은 됩니다. //하지만 조금 더 나은 것으로 대신해야 할 것입니다. static String crypt(String input, int offset) { StringBuffer sb = new StringBuffer(); for (int n=0; n<input.length(); n++) { sb.append((char)(offset+input.charAt(n))); } return sb.toString(); } // 실제 응용프로그램에서는 패스워드로의 액세스를 동조하도록 하면서 // 페스워드를 System.out 로 출력해서는 안됩니다! private void writeObject(ObjectOutputStream stream) throws IOException { password = crypt(password, 3); System.out.println("Password encyrpted as " + password); stream.defaultWriteObject(); password = crypt(password, -3); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); password = crypt(password, -3); System.out.println("Password decrypted to " + password); } public Person(String firstName, String lastName, String password, int age) { this.fullName = lastName + ", " + firstName; this.password = password; this.age = age; } public String toString() { return new String(fullName + " age " + age); } }
// readObject를 여기의 새 버젼으로 교체하십시오. private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException { ObjectInputStream.GetField gf = ois.readFields(); // 새로운 버젼을 가지고 있기를 바라며.... fullName = (String) gf.get("fullName", null); if (fullName == null) { // 이런. 구버젼입니다. fullName을 만들어냅니다. String lastName = (String) gf.get("lastName", null); String firstName = (String) gf.get("firstName", null); fullName = lastName + ", " + firstName; } age = gf.get("age", 0); password = (String) gf.get("password", null); password = crypt(password, -3); System.out.println("Password decrypted to " + password); }
콘솔상에서 javac dbcon.java 처럼 컴파일을 할경우
다른 클레스 에서 dbcon.class 를 사용 하려 하면, 오류가 발생한다.
즉 바로 접근 하지 못하기 때문이다.
디렉토리 단위로 컴파일을 하면 해당 디렉토리 안에 있는 클레스 파일들은 모두 사용 할 수 있다.
javac -d . dbcon.java
.<-이 현제 대렉토리라는 것을 나타냄
1. [C:\Program Files\Java\jdk1.6.0_11\jre\lib\ext] 디렉토리에 폴더에
파일을 붙 넣음
dbcon.java
mysql-connector-java-5.1.7-bin.jar
2. 소스 코드 실행 접속 되면 ok라고 메시지 출력
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class dbcon
{
Connection conn = null;
String host = "localhost", database ="phpmyadmin";
int port = 3306;
public dbcon()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection( "jdbc:mysql://" + host + ":" + port +"/" + database, "root", "" );
// ID, PW부분은 사용자에 따라서 입력.
System.out.println( " 성공!!");
} catch( SQLException e)
{
e.printStackTrace();
} catch(Exception e)
{
e.printStackTrace();
}
}
public static void main( String[] args )
{
dbcon DB = new dbcon();
}
}
1> http://java.sun.com/javase/downloads/index.jsp 에서 jdk 를 다운받아 설치한다
지금 최신버전인 jdk-6u11-windows-i586-p.exe 를 설치하였다.
기본으로 그냥 C:\Program Files\ 에 설치했다.
2> 내컴퓨터-속성-고급-환경변수 로들어가서 시스템 변수에..
변수이름 : JAVA_HOME
변수값 : JDK설치폴더 예>C:\Program Files\Java\jdk1.6.0_11
변수이름 : PATH (path는 기존 변수에 추가 한다. 구분자는 ;)
변수값 : JDK설치폴더\bin;
변수이름 : CLASSPATH
변수값 : JRE설치폴더\LIB; 예> C:\Program Files\Java\jdk1.6.0_11\lib\tools.jar
를 추가해준다
command 창에서 java -version 했을때 나오면 정상설치완료
3> http://tomcat.apache.org 에서 tomcat 5.5.x 버젼을 다운받는다.
(apache-tomcat-6.0.18 설치했다가, 재시작이 안돼서 지우고 apache-tomcat-5.5.27 설치했더니 잘 된다.)
apache-tomcat-5.5.27.exe 설치한다. (기본폴더에 그냥 설치했다.)
4. 내컴퓨터-속성-고급-환경변수-시스템변수 로들어가서
를 추가한다.
4> root 경로 바꾸기
C:\Program Files\Apache Software Foundation\Tomcat 5.5\conf\server.xml
을 열어서
host 사이에
<Context path="" docBase= "D:\WEB\~경로~" debug="0" reloadable="true" crossContext="true"/>
태그를 넣는다.
5> 재시작한다.
post 방식을 통해서 넘오온 인자값을 처리 하기 위해서
될 수 있으면 인코딩을 해주는 것이 좋다. 그래야 한글 깨짐 현상이 나타나지 않음
request.setCharacteEncoding("euc-kr");
<-- 요청한 파라미터의 캐릭터 인코딩을 한글(euc-kr)로 지정 한다. 이렇게 해야 한글을 올바르게 처리할 수 있다. -->