实践出真知,还得要多动手才行。今天做的放上来,实现了客户端的登陆功能,慢慢加功能,锻炼自己的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){
}
}
}
运行结果:
自己水平有限,做的这个小东西实在拿不出手,但还是对自己有些帮助,我会继续努力的,
分享到:
相关推荐
APK逆向--简单练练-附件资源
C#做的练练看
日语能力测试2级听力练练练1-10期
安卓应用-健康医疗-练练手机客户端 v3.1 安卓版.zip
Cocos2d-x3.0正式版 练练看 源码。有注释。
2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。...
《数据库系统概论》高等教育出版社 王珊 萨师煊编著 练习题及答案
基于cocos2d-js开发的练练看小游戏.zip
练习一下,算法不管什么时候都是非常重要的吧!呵呵,没事的时候做做也是很有意思的!!
狂练
使用oger做的练练看游戏,里面用到了CEGUI做界面,FMOD播放声音,OIS控制输入,免费给大家分享,有任何问题请邮件: xieaiwanli@hotmail.com
NULL 博文链接:https://zc0604.iteye.com/blog/1291130
平时作业,Java实现连连看,适合于初学者Java编程的练习。
全国城市英语角汇总,基本上大中城市都有哦,去练练你的口语吧
高仿练练开机数据录入
是一个VBA编写的游戏,有了VBA,EXCEL可以做很多事。
数据库附加上去即可用,就为能下载少分随便上传个
简单的ASP.NET图书管理系统—课程设计,有主目录代码和sql脚本文件,可以给新手练练
小弟学生党一枚,出于兴趣自学iOS一段时间了,所以做了这个小app练练手,也问过大家不少很新手的问题,很感谢。希望大家多多关照! 有同样愿意交流的新手朋友也可以联系我,大家一起进步哈~~ 测试环境:Xcode 6.2...