十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍了java通过JFrame做一个登录系统的界面完整代码示例,具有一定借鉴价值,需要的朋友可以参考下。
成都创新互联公司专业提供成都主机托管四川主机托管成都服务器托管四川服务器托管,支持按月付款!我们的承诺:贵族品质、平民价格,机房位于中国电信/网通/移动机房,郑州服务器托管服务有保障!
在java的JFrame内通过创建匿名对象的方式做登录界面
package com.sxt;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginFrame extends JFrame{
JTextField txtname=new JTextField();
JPasswordField txtpass=new JPasswordField();
JButton bl=new JButton("登录");
JButton bg=new JButton("关闭");
//构造无参构造器把主要的方法放在构造器里,然后在main方法里面调
public LoginFrame(){
setBounds(25,25,250,250);
Container c = getContentPane();
c.setLayout(new GridLayout(4,2,10,10));
c.add(new JLabel("用户名"));
c.add(txtname);
c.add(new JLabel("密码"));
c.add(txtpass);
c.add(bl);
c.add(bg);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//注意:此处是匿名内部类
bg.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
}
);
//注意:此处是匿名内部类
bl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
要使用Java编写超市购物程序,您需要使用Java数据库编程技术来连接到您的数据库并执行相关的数据库操作。在Java中,您可以使用JDBC(Java数据库连接)API来连接到数据库并执行SQL语句。例如,您可以使用JDBC API来执行以下操作:
创建数据库连接
执行增删改查(CRUD)操作
提交或回滚事务
执行存储过程和函数
查询数据库元数据
此外,您还可以使用Java的面向对象编程技术来封装数据库操作,以便更方便地在您的程序中使用。例如,您可以创建一个类来表示超市商品,并定义一些方法来执行商品的增删改查操作。这样,您就可以在程序中通过调用这些方法来简单地完成对数据库的操作,而不需要编写复杂的SQL语句。
哎,我还是自己代码黏贴给你吧!免得总通不过!装不下这么多字,详情自己找我。 连接数据库类(添加修改会员信息) package com.to.java; import java.util.Scanner; import java.sql.*; public class Vds { Scanner Input=new Scanner(System.in); ZhuCK1 zhuck1=new ZhuCK1(); public void x(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); System.out.println("----------查询结果---------"); System.out.println(); System.out.println("会员号--密码"); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } con.close(); stmt.close(); }catch(Exception e){ System.out.println(e);} System.out.println("是否返回上级菜单"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("欢迎下次再次使用");System.exit(-1);}; break; }} public void j(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } Scanner input=new Scanner(System.in); System.out.println("请输入会员信息:"); System.out.println("请输入会员号:"); String f=input.next(); System.out.println("请输入密码:"); String q=input.next(); String sqlst="insert into gg values('"+f+"','"+q+"')"; Statement tatement=con.createStatement(); int result=tatement.executeUpdate(sqlst); System.out.println("操作成功!"); con.close(); stmt.close(); }catch(Exception e){ System.out.println(e); } System.out.println("是否返回上级菜单"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("欢迎下次再次使用");System.exit(-1);}; break; } } public void xg(){ try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select id,mm from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); System.out.println(id+" "+mm); } Scanner input=new Scanner(System.in); System.out.println("请输入会员信息:"); System.out.println("请输入要修改的会员号:"); String f=input.next(); System.out.println("请输入修改的密码:"); String q=input.next(); String sqlst="update gg set mm='"+q+"' where id='"+f+"'"; Statement tatement=con.createStatement(); int result=tatement.executeUpdate(sqlst); System.out.println("操作成功!"); con.close(); stmt.close(); }catch(Exception e){ System.out.println(e); } System.out.println("是否返回上级菜单"); System.out.println("1.是"); System.out.println("2.否"); int m=Input.nextInt(); switch(m){ case 1: zhuck1.ZhucK1(); break; case 2: { System.out.println("欢迎下次再次使用");System.exit(-1);}; break; } } } package com.to.java; import java.util.Scanner; public class KeHu { Vds sj=new Vds(); public void kehu1(){ Scanner Input=new Scanner(System.in); ZhuCK1 zhuck1=new ZhuCK1(); System.out.println("******************************************************"+"\n\n"); System.out.println("1.显示所有客户信息"); System.out.println("2.添加客户信息"); System.out.println("3.修改客户信息"); System.out.println("4.查询客户信息"); System.out.println("5.返回上级菜单"); System.out.println("******************************************************"+"\n\n"); System.out.println("请输入数字:"); int a=Input.nextInt(); switch(a){ case 1: sj.x(); break; case 2: sj.j(); break; case 3: sj.xg(); break; case 4: sj.x();break; case 5: zhuck1.ZhucK1();break; } } } package com.to.java; public class GouWuXiTong { public static void main(String[] args){ ZhuCK zhuck=new ZhuCK(); zhuck.ZhucK(); } } package com.to.java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class ZhuCK { Scanner Input=new Scanner(System.in); public void ZhucK(){ ZhuCK1 zhuck1=new ZhuCK1(); System.out.println("\t\t"+"欢迎使用我心我素购物系统"); System.out.println("******************************************************"+"\n\n"); System.out.println("\t\t"+"1.登陆系统"); System.out.println(); System.out.println("\t\t"+"2.退出"+"\n\n"); System.out.println("******************************************************"); int d=1; while(d!=2){ System.out.println("请输入会员号:"); String p=Input.next(); System.out.println("请输入密码:"); String f=Input.next(); try{ Connection con=null; Statement stmt=null; ResultSet rs=null; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String ur1="jdbc:odbc:gg"; con=DriverManager.getConnection(ur1); stmt=con.createStatement(); String sqlstmt="select * from gg"; rs=stmt.executeQuery(sqlstmt); while(rs.next()){ String id=rs.getString("id"); String mm=rs.getString("mm"); if(id.equals(p)mm.equals(f)){ { System.out.println("请输入数字:"); int a=Input.nextInt(); switch(a){ case 1: zhuck1.ZhucK1(); break; case 2: break; }d=2; } } } }catch(Exception e){ System.out.println(e);} {System.out.println("输入错误请再次输入:");d=1;} } }} }
1.index.jsp登陆界面:
%@ page language="java" import="java.util.*" pageEncoding="GB2312"%
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
%session.invalidate();% %--销毁所有session对象--%
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
base href="%=basePath%"
title购物车/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
body
center
hr
请输入用户名,默认的为Guest
form action="checklogin.jsp" mothod=get
table width="40%" border="1"
tr bgcolor="#336600"
tddiv align="center"font color="FFFFFF"用户登陆/font/div/td
/tr
tr align="center" bgcolor="#CCCCCC"
td用户名:input type="password" name="userID"/td
/tr
tr align="center" bgcolor="#CCCCCC"
td口 令:input type="password" name="password"/td
/tr
tr align="center" bgcolor="#CCCCCC"
tdinput type="submit" value="登陆"/td
/tr
/table
/form
/center
/body
/html
2.checklogin.jsp登陆认证页面:
%@ page language="java" import="java.util.*" pageEncoding="GB2312"%
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
jsp:useBean id="Car" class="web.Car" scope="session"
jsp:setProperty property="*" name="Car"/
/jsp:useBean
%session.setMaxInactiveInterval(900); % %--设置session超时为30分--%
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
base href="%=basePath%"
titleMy JSP 'checklogin.jsp' starting page/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
body
%
String nextpage;
if(Car.getUserID().equals("Guest"))
nextpage="car.jsp";
else
nextpage="index.jsp";
%
jsp:forward page="%=nextpage%"/jsp:forward
/body
/html
3.car.jsp购物车页面
%@ page language="java" import="java.util.*" pageEncoding="GB2312"%
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
%@ page import="java.util.*" %
%@ page import="web.Car" %
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
base href="%=basePath%"
title购物车/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
body
br%@ include file="header.jsp" %
hr
font size="2"
jsp:useBean id="Car" class="web.Car" scope="session"
/jsp:useBean
pfont color="#804040" face="楷体_GB2312"
strong百货商场,请尽情的选购商品添加到购物车!/strong
/font
%String str=response.encodeRedirectURL("add.jsp"); %
form action="%=str %" method="post" name="form"
select name="item" value="没选择"
option value="TV"电视机/option
option value="apple"苹果/option
option value="coke"可口可乐/option
option value="milk"牛奶/option
option value="tea"茶叶/option
/select
pfont color="#804040" face="楷体_GB2312"
输入购买的数量:
/font
input type="text" name="mount"
p
input type="radio" name="unit" value="个"个
input type="radio" name="unit" value="公斤"公斤
input type="radio" name="unit" value="台"台
input type="radio" name="unit" value="瓶"瓶p
input type="submit" value="提交添加"
/form
pfont color="#804040" face="楷体_GB2312"你的购物车里有如下商品:/font
font color="#FF8040" size="2"
%
Hashtable list=Car.list_h();
Enumeration enums=list.elements();
while(enums.hasMoreElements()){
String goods=(String) enums.nextElement();
byte b[]=goods.getBytes("ISO-8859-1");
goods=new String(b);
out.println("br"+goods);
}
%
/font
% String strl=response.encodeRedirectURL("selectRemoveGoods.jsp");%
form action="%=strl %" method="post" name="form"
input type="submit" value="修改购物车中的商品"
/form
/font
%@ include file="tail.jsp" %
/body
/html
4.add.jsp 增加物品:
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
base href="%=basePath%"
title购物车/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
body
%@ include file="header.jsp" %
hr
font size="2"
jsp:useBean id="Car" class="web.Car" scope="session"/jsp:useBeanbr
jsp:setProperty name="Car" property="*"/
%Car.add_h();%
font face="楷体_GB2312"
font color="#FF8040" size="2"
p您的购物车有如下商品:
%
Hashtable list=Car.list_h();
Enumeration enums=list.elements();
while(enums.hasMoreElements()){
String goods=(String) enums.nextElement();
byte b[]=goods.getBytes("ISO-8859-1");
goods=new String(b);
out.println("br"+goods);
}
%
/font
%String str=response.encodeRedirectURL("car.jsp"); %
br
form action="%=str %" method="post" neme="form"
input type="submit" value="继续购物"
/form
%String strl=response.encodeRedirectURL("selectRemoveGoods.jsp"); %
br
form action="%=strl %" method="post" neme="form"
input type="submit" value="修改购物车中的物品"
/form
/font
/font
%@ include file="tail.jsp"%
/body
/html
5.selectRemoveGoods.jsp选择删除商品:
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
%@ page import="java.util.*" %
%@ page import="web.Car" %
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
base href="%=basePath%"
title购物/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
body
br%@ include file="header.jsp" %
hr
jsp:useBean id="Car" class="web.Car" scope="session"
/jsp:useBeanbr
p选择从购物车中删除的物品:
%String str=response.encodeRedirectURL("removeWork.jsp"); %
form action="%=str %" method="post" name="form"
select name="deleteitem" size="1"
option value="TV"电视机/option
option value="apple"苹果/option
option value="coke"可口可乐/option
option value="milk"牛奶/option
option value="tea"茶叶/option
/select
input type="submit" value="提交删除"
/form
font face="楷体_GB2312"
font color="#FF8040" size="2"
p您的购物车有如下商品:
%
Hashtable list=Car.list_h();
Enumeration enums=list.elements();
while(enums.hasMoreElements()){
String goods=(String) enums.nextElement();
byte b[]=goods.getBytes("ISO-8859-1");
goods=new String(b);
out.println("br"+goods);
}
%
/font/font
%String strl=response.encodeRedirectURL("car.jsp"); %
form action="%=strl %" method="post" neme="form"
input type="submit" value="继续购物"
/form
%@include file="tail.jsp" %
/body
/html
6.removeWork.jsp删除页面:
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
%@ page import="java.util.*" %
%@ page import="web.Car" %
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
base href="%=basePath%"
title购物车/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
body
%@include file="header.jsp" %
hr
font size="2"
jsp:useBean id="Car" class="web.Car" scope="session"
/jsp:useBeanbr
%String str=response.encodeRedirectURL("removeWork.jsp"); %
%String name=request.getParameter("deleteitem");
if(name==null)
name="";
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
Car.dele_h(name);
out.println("您删除了货物"+name);%
/font
font face="楷体_GB2312"
font color="#FF8040" size="2"
p您的购物车有如下商品:
%
Hashtable list=Car.list_h();
Enumeration enums=list.elements();
while(enums.hasMoreElements()){
String goods=(String) enums.nextElement();
byte b[]=goods.getBytes("ISO-8859-1");
goods=new String(b);
out.println("br"+goods);
}
%
/font/font
%String strp=response.encodeRedirectURL("car.jsp"); %
form action="%=strp %" method="post" neme="form"
input type="submit" value="继续购物"
/form
%String strl=response.encodeRedirectURL("selectRemoveGoods.jsp"); %
form action="%=strl %" method="post" neme="form"
input type="submit" value="修改购物车中的物品"
/form
/body
/html
7.header.jsp页面头部:
%@ page language="java" import="java.util.*" pageEncoding="GB2312"%
center
========================================================================br
购物车系统br
=======================================================================br
WELCOME!
jsp:getProperty name="Car" property="userID"/
当前时间是:
%=new java.util.Date().toLocaleString() %
br
/center
8.tail页面尾部
%@ page language="java" import="java.util.*" pageEncoding="GB2312"%
center
hr
JSP+TOMCAT购物系统
/center
9.Car.java类
package web;
import java.util.*;
import java.io.*;
public class Car implements Serializable{
Hashtable list=new Hashtable(); //散列表,商品列表
String item="Welcome";
int mount=0; //商品数量
String unit=null; //商品单位
String userID; //用户
public void Car(){
}
public void setItem(String item) {
this.item = item;
}
public void setMount(int mount) {
this.mount = mount;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public Hashtable list_h() {
return list;
}
public void dele_h(String s) {
list.remove(s);
}
public void add_h(){
String str="Name:"+item+"Mount:"+mount+"Unit:"+unit;
list.put(item, str);
}
}
N种办法
1、mybatis 配置文件 写SQL 解决
2、通过JAVA service 通过父类信息查询子类并封装
3、提供一个接口,前台二级联动方式,点第一个给后台传值0 ,查询父ID为0 的 当点击第二个联动的时候 把第一个信息的ID 给你传过去,如 电子数码ID 为1 那就传1 后台还是根据父类查询,也就是 父类ID 1 这样就查出 所对应的 子类信息了。