`
dato0123
  • 浏览: 913997 次
文章分类
社区版块
存档分类
最新评论

新手练练----也做即时通信系统(1)

 
阅读更多
实践出真知,还得要多动手才行。今天做的放上来,实现了客户端的登陆功能,慢慢加功能,锻炼自己的j2se水平。。。功能太简单了(本人水平有限^o^)。
(一)客户端:
login.java
packagevitaminclient;

importjava.awt.*;

importjavax.swing.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowEvent;
importjava.awt.event.WindowAdapter;
importjava.net.*;
importjava.io.*;
importjava.util.*;


/***//**
*<p>Title:</p>
*
*<p>Description:</p>
*
*<p>Copyright:Copyright(c)2006</p>
*
*<p>Company:</p>
*
*
@authornotattributable
*
@version1.0
*/

publicclassLoginextendsJFrame
{
privateStringuserName="";//用户名
privateStringpassword="";//密码
privateSocketsocket=null;//客户端socket
privatejava.io.BufferedReaderin=null;//读数据的
privatejava.io.PrintWriterout=null;//向服务器写数据
privatestaticfinalintSeverPort=6018;//服务器端口
privateStringclientCmd="";
privateStringserverMsg="";

publicLogin(){
try{
jbInit();
}
catch(Exceptionexception){
exception.printStackTrace();
}

}


privatevoidjbInit()throwsException{
getContentPane().setLayout(
null);
jPanel1.setBounds(
newRectangle(0,0,435,327));
jPanel1.setLayout(
null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("登陆");


btnLogin.addActionListener(
newLogin_btnLogin_actionAdapter(this));
btnReset.addActionListener(
newLogin_btnReset_actionAdapter(this));

this.getContentPane().add(jPanel1);
tfName.setBounds(
newRectangle(139,75,178,41));
jLabel2.setText(
"密码:");
jLabel2.setBounds(
newRectangle(56,162,74,38));
tfPassword.setBounds(
newRectangle(137,156,182,41));
btnLogin.setBounds(
newRectangle(90,254,87,32));
btnLogin.setText(
"登陆");
btnReset.setBounds(
newRectangle(238,251,84,34));
btnReset.setText(
"重置");
jPanel1.add(jLabel1);
jPanel1.add(jLabel2);
jPanel1.add(tfName);
jPanel1.add(tfPassword);
jPanel1.add(btnLogin);
jPanel1.add(btnReset);
jLabel1.setText(
"用户名:");
jLabel1.setBounds(
newRectangle(56,76,71,38));
this.setLocation(310,200);
this.setSize(400,400);
}


publicstaticvoidmain(String[]args){
Loginlogin
=newLogin();
login.setVisible(
true);
}


JPaneljPanel1
=newJPanel();
JLabeljLabel1
=newJLabel();
JTextFieldtfName
=newJTextField();
JLabeljLabel2
=newJLabel();
JPasswordFieldtfPassword
=newJPasswordField();
JButtonbtnLogin
=newJButton();
JButtonbtnReset
=newJButton();
publicvoidbtnLogin_actionPerformed(ActionEvente)
{//用户登陆
intnumRead;
if(tfName.getText().trim().length()==0)
{
javax.swing.JOptionPane.showMessageDialog(
this,"请输入用户名!!!","用户登陆",JOptionPane.WARNING_MESSAGE);
return;
}

if(tfPassword.getPassword().length==0)
{
javax.swing.JOptionPane.showMessageDialog(
this,"请输入密码!!!","用户登陆",JOptionPane.WARNING_MESSAGE);
return;
}

this.userName=tfName.getText().trim();//获取用户名
this.password=String.valueOf(tfPassword.getPassword());//获取密码
this.clientCmd="login"+this.userName+""+this.password;

//this.clientCmd="login";
try
{
this.socket=newSocket(InetAddress.getLocalHost(),SeverPort);//连接服务器
this.in=newBufferedReader(newInputStreamReader(socket.getInputStream()));//从服务器读数据的
this.out=newPrintWriter(newBufferedWriter(newOutputStreamWriter(socket.getOutputStream())),true);//向数据库写数据的
out.println(this.clientCmd);

this.serverMsg=in.readLine();
if(this.serverMsg.equals(newString("LoginBad")))
{
javax.swing.JOptionPane.showMessageDialog(
this,"登陆失败!!!");
return;
}

elseif(this.serverMsg.equals(newString("LoginGood")))
{
vitaminclient.clientMaincm
=newclientMain();
cm.setSize(
200,500);
cm.setVisible(
true);
this.dispose();

}


}

catch(java.io.IOExceptionex)
{
System.err.println(ex.getMessage().toString());
ex.printStackTrace();
}

catch(java.lang.Exceptionex)
{
System.err.println(ex.getMessage().toString());
ex.printStackTrace();
}

}

publicvoidbtnReset_actionPerformed(ActionEvente){
this.tfName.setText("");
this.tfPassword.setText("");
}


}



classLogin_btnReset_actionAdapterimplementsActionListener{
privateLoginadaptee;
Login_btnReset_actionAdapter(Loginadaptee)
{
this.adaptee=adaptee;
}


publicvoidactionPerformed(ActionEvente){
adaptee.btnReset_actionPerformed(e);
}

}








classLogin_btnLogin_actionAdapterimplementsActionListener{
privateLoginadaptee;
Login_btnLogin_actionAdapter(Loginadaptee)
{
this.adaptee=adaptee;
}


publicvoidactionPerformed(ActionEvente){
adaptee.btnLogin_actionPerformed(e);
}

}


clientMain.java(开发中。。。)

(二)服务器端
数据库用的是Access,图个简单,反正是做着练手用的,能偷懒就尽量吧。。。。
业务逻辑和数据访问分开的,数据访问我封装了一个javaBean类来实现:
DBbase.java
packagecom.vitamin.DataAccess;

importjava.sql.*;

publicclassDBbase{
StringsDBDriver
="sun.jdbc.odbc.JdbcOdbcDriver";
StringsConnstr
="jdbc:odbc:myDB";
Connectionconnect
=null;
ResultSetrs
=null;
Statementstmt
=null;


publicDBbase()
{

try
{

Class.forName(sDBDriver);

}

catch(ClassNotFoundExceptionex)
{
System.err.println(ex.getMessage());

}

}

publicResultSetexecuteQuery(Stringsql)
{

try
{
this.connect=DriverManager.getConnection(sConnstr);
this.stmt=this.connect.createStatement();
rs
=stmt.executeQuery(sql);
}

catch(SQLExceptionex)
{
System.err.println(ex.getMessage());
}

returnrs;
}

publicintexecuteUpdate(Stringsql)
{
intresult=0;
try
{
this.connect=DriverManager.getConnection(sConnstr);
this.stmt=this.connect.createStatement();
result
=stmt.executeUpdate(sql);
}

catch(SQLExceptionex)
{
System.err.println(ex.getMessage());
}

returnresult;
}


}



服务器端
为了简单,连GUI都不弄了,等后期再来完善吧,先把主要功能做出来再说:
server.java
packagecom.vitamin.vitaminserver;

importjava.net.*;
importjava.io.*;

/***//**
*<p>Title:</p>
*
*<p>Description:</p>
*
*<p>Copyright:Copyright(c)2006</p>
*
*<p>Company:</p>
*
*
@authornotattributable
*
@version1.0
*/

publicclassserver
{



publicstaticvoidmain(String[]args)throwsjava.io.IOException
{
java.net.ServerSockets
=newServerSocket(6018);
System.out.println(
"服务器启动:"+s);
try
{
while(true)
{
java.net.Socketsocket
=s.accept();
System.out.println(
"连接接受"+socket);
try
{
newServerThread(socket);
}

catch(java.io.IOExceptionex)
{
socket.close();
}


}

}

catch(java.lang.Exceptionex)
{
System.err.println(ex.getMessage().toString());
ex.printStackTrace();
}

finally
{
s.close();
}


}





}


ServerThread.java
packagecom.vitamin.vitaminserver;

importjava.io.*;
importjava.net.*;
importjava.util.*;
importcom.vitamin.DataAccess.*;


/***//**
*<p>Title:</p>
*
*<p>Description:</p>
*
*<p>Copyright:Copyright(c)2006</p>
*
*<p>Company:</p>
*
*
@authornotattributable
*
@version1.0
*/

publicclassServerThreadextendsjava.lang.Thread
{
privatejava.net.Socketsocket=null;
privatejava.io.BufferedReaderin=null;//读数据的
privatejava.io.PrintWriterout=null;//向客户写数据
privateStringclientMsg="";
privateStringsql="";
privatejava.sql.ResultSetrs=null;

publicServerThread()
{
super();
}

publicServerThread(Sockets)throwsjava.io.IOException
{
this.socket=s;
this.in=newBufferedReader(newInputStreamReader(this.socket.getInputStream()));
this.out=newPrintWriter(newBufferedWriter(newOutputStreamWriter(this.socket.getOutputStream())),true);
this.start();//启动线程
}


publicvoidrun()
{
String[]msgTmp;
Stringspliter
="";
try
{
while(true)
{
this.clientMsg=in.readLine();
System.out.println(
this.clientMsg);
msgTmp
=this.clientMsg.split(spliter);
System.out.println(msgTmp[
0]);
if(msgTmp[0].equals(newString("login")))
{
Stringname
="";
Stringpwd
="";
System.out.println(
this.clientMsg);
name
=msgTmp[1];
pwd
=msgTmp[2];
com.vitamin.DataAccess.DBbasemyDb
=newDBbase();
this.sql="selectcount(*)ascountfromuserswhereusername='"+name+"'andpassword='"
+pwd+"'";
this.rs=myDb.executeQuery(this.sql);
intresult=0;
if(rs.next())
{
result
=rs.getInt("count");
}

if(result>=1)
{
this.out.println("LoginGood");
}

else
{
this.out.println("LoginBad");
}



}

else
{


}


}

}

catch(java.lang.Exceptionex)
{
System.out.println(ex.getMessage().toString());
ex.printStackTrace();
}

try{
this.socket.close();
}
catch(IOExceptionex1){
}

}

}


运行结果:
6-20-1.GIF
6-20-2.GIF
6-20-3.GIF

自己水平有限,做的这个小东西实在拿不出手,但还是对自己有些帮助,我会继续努力的,
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics