자바 JDBC + Webbrowser + RMI + JSP + Beans 를 이용한 Server Client 로또 번호 발생 프로그램
1. 차례
1.1 구현 기술
1.2 설명
1.2.1 프로그램 소개
1.2.2 전체 구현 그림
1.2.3 클래스 다이어그램
1.2.4 시퀀스 다이어그램
1.3 인터페이스
1.4 소스 코드
2. 구현기술
RMI
JSP
Beans
JDBC
Linux
Tomcat on Apache (아파치에 모듈로 톰켓을 얹음.)
Oracle 9i
3. 설명
3.1 프로그램 소개
랜덤으로 로또 번호를 발생하는 프로그램이다.
서버와 클라이언트는 RMI를 통해 통신을 하며 클라이언트에서 init(), event(), sort(), setName(), getNums()라는 메소드를 CALL 하게 된다. 발생된 번호는 JDBC를 사용하여 DB서버에 발생된 번호와 사용자이름이 저장되며, 조회를 원할 경우 프로그램 우측 화면에 있는 웹브라우저를 통해 JSP를 이용해 DB Connect부분이 구현된 BEANS와 통신해 결과를 출력하게 된다. 여기서 Beans를 사용한 이유는 DB Connect부분을 컴파일을해서 보관하기 때문에 보안상 안전기 때문이다.
3.2 전체 구현 그림
3.2 클래스 다이어그램
3.2.1 Lotto.java 클래스 다이어그램
3.2.2 LottoImpl.java 클래스 다이어그램
3.2.3 LottoClient.java 클래스 다이어그램
3.2.4 LottoLotto3.jsp 클래스 다이어그램
3.2.5 Lottobean.java 클래스 다이어그램
3.2.6 lotto table의 클래스 다이어그램
3.3 시퀀스 다이어그램
4. 인터페이스
5. 소스코드
5.1 Lotto.java
import java.rmi.*;
public interface Lotto extends Remote{
void init() throws RemoteException;
void event() throws RemoteException;
void sort() throws RemoteException;
int[] getNums() throws RemoteException;
void setName(String n) throws RemoteException;
}
|
5.2 LottoImpl.java
import java.rmi.*;
import java.util.*;
import java.rmi.server.*;
import java.sql.*;
public class LottoImpl extends UnicastRemoteObject implements Lotto{
int[] gong;
int[] lotto;
Random ran;
int random;
int num;
String name;
Connection con;
Statement stmt;
ResultSet rs;
String url = "jdbc:oracle:thin:@203.246.80.54:1521:darackdb";
String username = "leejung0";
String passwd = "xxxxxxx"; //비밀번호 밝힐 수 없습니다. ^^;
public LottoImpl() throws RemoteException{
gong = new int[45];
lotto = new int[6];
for (int i =0;i<45;i++){
gong[i]=i+1;
}
ran = new Random();
}
public void setName(String n) throws RemoteException{
name = n;
}
public void init() throws RemoteException{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url,username,passwd);
con.setAutoCommit(true);
stmt = con.createStatement();
} catch (ClassNotFoundException e1) {
System.out.println(e1);
}
catch (SQLException e) {
System.out.println(e);
}
for (int i =0;i<45;i++){
gong[i]=i+1;
}
}
public void event() throws RemoteException{
for(int i=0;i<6;i++){
random = Math.abs(ran.nextInt()%(gong.length-i));
lotto[i]=gong[random];
for(int j=random;j<45;j++){
if (j>43)
gong[j]=0;
else
gong[j]=gong[j+1];
}
}
}
public void sort() throws RemoteException{
int temp=0;;
String a;
for(int i=0;i<6;i++)
for(int j=i;j<6;j++)
if (lotto[i]>lotto[j]){
temp = lotto[i];
lotto[i]=lotto[j];
lotto[j]=temp;
}
try{
a = "insert into lotto(name,n1,n2,n3,n4,n5,n6) values('" + name + "','" + lotto[0] +"','" + lotto[1] + "','" + lotto[2] + "','" + lotto[3] + "," + lotto[4] + "','" + lotto[5] + "')";
stmt.execute(a);
stmt.close();
con.close();
}catch (Exception e){
System.out.println("에러");
}
}
public int[] getNums() throws RemoteException{
return lotto;
}
public void setName(String n) throws RemoteException{
name = n;
}
public static void main(String args[]){
try{
LottoImpl l = new LottoImpl();
Naming.rebind("LottoServer",l);
}catch(Exception e){
System.out.println("Exception::" + e);
}
}
}
|
5.3 LottoClient.java
import java.rmi.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.TableColumn;
import java.beans.PropertyVetoException;
public class LottoClient extends JFrame implements ActionListener{
Toolkit thekit = getToolkit();
Dimension wndSize = thekit.getScreenSize();
Dimension frameSize;
JPanel jPanel = new JPanel();
JTextField textName = new JTextField();
JTextField textNums = new JTextField();
JLabel labelName = new JLabel();;
JLabel labelNums = new JLabel();
JButton buttonOK = new JButton();
JButton buttonSerch = new JButton();
String colNames[]={"첫번째","두번째","세번째","네번째","다섯번째","여섯번째"};
String data[][]={{"","","","","",""}};
JTable tableList ;
JScrollPane scroll;
JEditorPane browser;
JScrollPane scroll2;
JMenuItem exit;
JMenuBar mb = new JMenuBar();
int[] lottonums = new int[6];
String[][] lottonums2;
static String serverURL;
int dis = 0;
int dis2 = 0;
public LottoClient(){
setBounds(0,0,wndSize.width,wndSize.height);
jPanel.setLayout(null);
jPanel.setBounds(0,0,wndSize.width,wndSize.height);
frameSize = getSize();
setTitle("로또 번호 발생기");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
labelName.setText("이름 : ");
labelNums.setText("횟수 :");
buttonOK.setText("확인");
buttonSerch.setText("조회");
buttonOK.addActionListener(this);
buttonSerch.addActionListener(this);
JMenu file = new JMenu("System");
file.add( exit = new JMenuItem("EXIT"));
exit.addActionListener(this);
mb.add(file);
setJMenuBar(mb);
view();
this.setVisible(true);
}
public void view(){
labelName.setBounds(new Rectangle(50, 50, 70, 30));
labelNums.setBounds(new Rectangle(50, 100, 70, 30));
textName.setBounds(new Rectangle(150, 50, 70, 30));
textNums.setBounds(new Rectangle(150, 100, 70, 30));
buttonOK.setBounds(new Rectangle(150, 150, 70, 30));
buttonSerch.setBounds(new Rectangle(250, 150, 70, 30));
if (dis2==1){
scroll2.setBounds(new Rectangle(500, 50, 450, 500));
jPanel.add(scroll2,null);
}
if (dis==1){
scroll.setBounds(new Rectangle(50,200,400,300));
jPanel.add(scroll,null);
}
jPanel.add(labelName,null);
jPanel.add(labelNums,null);
jPanel.add(textName,null);
jPanel.add(textNums,null);
jPanel.add(buttonOK,null);
jPanel.add(buttonSerch,null);
getContentPane().add(jPanel);
textName.requestFocus(true);
}
public static void main(String args[]){
LottoClient lc = new LottoClient();
if(args.length < 1){
System.out.println("Specity IP ");
System.exit(1);
}
serverURL = "rmi://" + args[0] + "/LottoServer";
}
public void actionPerformed(ActionEvent e){
int count=0;
String cmd = e.getActionCommand();
lottonums2 = new String[Integer.parseInt(textNums.getText())][6];
if (cmd.equals("확인")){
if ((textName.getText()!="") && (textNums.getText()!="")){
try{
Lotto l = (Lotto)Naming.lookup(serverURL);
for(int i=0;i<Integer.parseInt(textNums.getText());i++){
l.setName(textName.getText());
l.init();
l.event();
l.sort();
lottonums = l.getNums();
lottonums2[i][0]=String.valueOf(lottonums[0]);
lottonums2[i][1]=String.valueOf(lottonums[1]);
lottonums2[i][2]=String.valueOf(lottonums[2]);
lottonums2[i][3]=String.valueOf(lottonums[3]);
lottonums2[i][4]=String.valueOf(lottonums[4]);
lottonums2[i][5]=String.valueOf(lottonums[5]);
}
if(dis==1){
scroll.setVisible(false);
}
dis = 1;
tableList = new JTable(lottonums2,colNames);
scroll = new JScrollPane(tableList);
view();
scroll.setVisible(true);
}
catch(Exception e1){
System.out.println("Exception: " + e1);
}
}
}
else if (cmd.equals("EXIT")){
System.exit(1);
}
else if (cmd.equals("조회")){
try{
if(dis2==1){
scroll2.setVisible(false);
}
dis2 = 1;
browser = new JEditorPane();
scroll2 = new JScrollPane(browser);
browser.setPage("http://www.darack.net/~leejung0/lotto3.jsp?names="+textName.getText());
view();
scroll2.setVisible(true);
}catch(Exception e2){}
}
}
}
|
5.4 Lotto3.jsp
<%@ page import="lotto.Lottobean" contentType="text/html;charset=euc-kr"%>
<jsp:useBean id="lottobean" class="lotto.Lottobean" scope="page" />
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=euc-kr">
<meta name="generator" content="Namo WebEditor v5.0">
</head>
<body bgcolor="white" text="black" link="blue" vlink="purple" alink="red">
<p align="left"> </p>
<%! int[] nums; String[][] get;
int input; int size;%>
<%
String names = new String(request.getParameter("names").trim().getBytes("ISO-8859-1"),"euc-kr");
%>
<% lottobean.setName(names); size=lottobean.getSize(); get=lottobean.getNums(); lottobean.setNull();%>
<%=names%> <p>
<table border=1> <% for(int i=0;i<size;i++){%>
<tr><td width=50> <%= get[i][0] + " "%>
</td><td width=50> <%= get[i][1] + " "%>
</td><td width=50> <%= get[i][2] + " "%>
</td><td width=50> <%= get[i][3] + " "%>
</td><td width=50> <%= get[i][4] + " "%>
</td><td width=50> <%= get[i][5]%>
</td></tr> </p>
<% }%>
</table><p>당첨되면 알지? leejung0@snut.ac.kr 연락주고 </p
</body>
</html>
|
5.5 Lottobean.java
package lotto;
import java.sql.*;
public class Lottobean
{
Connection con;
Statement stmt;
ResultSet rs;
String url = "jdbc:oracle:thin:@203.246.80.54:1521:darackdb";
String username = "leejung0";
String passwd = "xxxxxxx";
String[][] getnum;
int count=0;
int size;
String name;
public Lottobean(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url,username,passwd);
stmt = con.createStatement();
} catch (ClassNotFoundException e1) {
System.out.println(e1);
}
catch (SQLException e) {
System.out.println(e);
}
}
public String[][] getNums(){
return getnum;
}
public int getSize(){
return size;
}
public void setNull(){
getnum=null;
count=0;
size=0;
}
public void setName(String n){
name = n;
try{
rs = stmt.executeQuery("select n1,n2,n3,n4,n5,n6 from lotto where name='"+name+"'");
while(rs.next()){
count++;
}
size=count;
rs = stmt.executeQuery("select n1,n2,n3,n4,n5,n6 from lotto where name='"+name+"'");
getnum = new String[count][6];
count=0;
while(rs.next()){
count++;
getnum[count-1][0] = rs.getString(1);
getnum[count-1][1] = rs.getString(2);
getnum[count-1][2] = rs.getString(3);
getnum[count-1][3] = rs.getString(4);
getnum[count-1][4] = rs.getString(5);
getnum[count-1][5] = rs.getString(6);
}
stmt.close();
rs.close();
con.close();
}catch(Exception e1){}
}
}
|
5.6 DataBase DDL
댓글
댓글 쓰기