导航:首页 > 源码编译 > javaweb简单项目源码

javaweb简单项目源码

发布时间:2024-12-01 04:01:50

❶ 求java web增删改查 极简源码

//用户新增
publicbooleanaddUser(Usersuser){
try{
conn=ConnDB.getConnection();
Stringsql="insertintotb_usersvalues(default,?,?,?,?,?,?)";
System.out.println(sql);
ps=conn.prepareStatement(sql);
ps.setInt(1,user.getDepID());
ps.setString(2,user.getUserName());
ps.setString(3,user.getUserPwd());
ps.setString(4,user.getUserCode());
ps.setString(5,user.getUserSex());
ps.setInt(6,user.getUserAge());
if(ps.executeUpdate()==1){
returntrue;
}
}catch(Exceptione){
e.printStackTrace();
}finally{//关闭当前页打开的相关对象
ConnDB.close(conn,ps,null);
}
returnfalse;
}
//用户删除
publicbooleandelUser(intid){
try{
conn=ConnDB.getConnection();
Stringsql="deletefromtb_userswhereid=?";
System.out.println(sql);
ps=conn.prepareStatement(sql);
ps.setInt(1,id);
if(ps.executeUpdate()==1){
returntrue;
}
}catch(Exceptione){
e.printStackTrace();
}finally{//关闭当前页打开的相关对象
ConnDB.close(conn,ps,null);
}
returnfalse;
}
//用户编辑
publicbooleanupdateUser(Usersuser){
try{
conn=ConnDB.getConnection();
Stringsql="updatetb_userssetdepID=?,userName=?,userPwd=?,userCode=?,userSex=?,userAge=?whereid=?";
System.out.println(user.getDepID()+user.getUserName()+user.getUserPwd()+user.getUserCode()+user.getUserSex()+user.getUserAge()+user.getId());
ps=conn.prepareStatement(sql);
ps.setInt(1,user.getDepID());
ps.setString(2,user.getUserName());
ps.setString(3,user.getUserPwd());
ps.setString(4,user.getUserCode());
ps.setString(5,user.getUserSex());
ps.setInt(6,user.getUserAge());
ps.setInt(7,user.getId());
if(ps.executeUpdate()==1){
returntrue;
}
}catch(Exceptione){
e.printStackTrace();
}finally{//关闭当前页打开的相关对象
ConnDB.close(conn,ps,null);
}
returnfalse;
}
//根据id查询用户
publicUsersfindAllUserById(intid){
Usersu=null;
DepDaodepd=null;
try{
conn=ConnDB.getConnection();
Stringsql="select*fromtb_userswhereid=?";
System.out.println(sql);
ps=conn.prepareStatement(sql);
ps.setInt(1,id);
rs=ps.executeQuery();
if(rs.next()){
depd=newDepDao();
Departmentdep=depd.findAllDepById(rs.getInt("depID"));
System.out.println(dep.getDepName());
u=newUsers();
u.setId(rs.getInt("id"));
u.setDepID(rs.getInt("depID"));
u.setUserName(rs.getString("userName"));
u.setUserPwd(rs.getString("userPwd"));
u.setUserCode(rs.getString("userCode"));
u.setUserSex(rs.getString("userSex"));
u.setUserAge(rs.getInt("userAge"));
u.setDep(dep);
}

}catch(Exceptione){
e.printStackTrace();
}finally{//关闭当前页打开的相关对象
ConnDB.close(conn,ps,rs);
}
returnu;
}

这是我在层写的代码,都调用了ConnDB这个类,这个类完成了驱动的注册,及连接数据库的功能,代码如下:

packagecom.asjy.util;

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;

publicclassConnDB{
privatestaticStringurl="jdbc:mysql://localhost:3306/news";
privatestaticStringuser="root";
privatestaticStringpass="root";

//1.加载驱动
static{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundExceptione){
System.out.println("驱动加载失败");
}
}


//2.建立数据库连接对象
()throwsException{
returnDriverManager.getConnection(url,user,pass);
}

//3.关闭数据库
publicstaticvoidclose(Connectionconn,Statementps,ResultSetrs){
try{
if(rs!=null){
rs.close();
rs=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLExceptione){
e.printStackTrace();
}
}
}

❷ 用java web小游戏源代码。期末结课老师让做,急用,谢了

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MainClass extends JFrame {
ControlSnake control;

Toolkit kit;

Dimension dimen;

public static void main(String[] args) {
new MainClass("my snake");
}

public MainClass(String s) {
super(s);
control = new ControlSnake();
control.setFocusable(true);
kit = Toolkit.getDefaultToolkit();
dimen = kit.getScreenSize();

add(control);
setLayout(new BorderLayout());
setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3
setSize(FWIDTH, FHEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}

public static final int FWIDTH = 315;

public static final int FHEIGHT = 380;
}

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class ControlSnake extends JPanel implements ActionListener {
Random rand;

ArrayList<Point> list, listBody;

String str, str1;

static boolean key;

int x, y, dx, dy, fx, fy, flag;

int snakeBody;

int speed;

public ControlSnake() {
snakeBody = 1;

str = "上下左右方向键控制 P键暂停...";
str1 = "现在的长度为:" + snakeBody;
key = true;
flag = 1;

speed = 700;
rand = new Random();
list = new ArrayList<Point>();
listBody = new ArrayList<Point>();

x = 5;
y = 5;
list.add(new Point(x, y));
listBody.add(list.get(0));

dx = 10;
dy = 0;

fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2

setBackground(Color.BLACK);
setSize(new Dimension(318, 380));

final Timer time = new Timer(speed, this);
time.start();

addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37) {
dx = -10;
dy = 0;
} else if (e.getKeyCode() == 38) {
dx = 0;
dy = -10;
} else if (e.getKeyCode() == 39) {
dx = 10;
dy = 0;
} else if (e.getKeyCode() == 40) {
dx = 0;
dy = 10;
} else if (e.getKeyCode() == 80) {
if (flag % 2 == 1) {
time.stop();
}
if (flag % 2 == 0) {
time.start();
}
flag++;
}
}
});

}

public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 400, 400);
g.setColor(Color.DARK_GRAY);
g.drawLine(3, 3, 305, 3);
g.drawLine(3, 3, 3, 305);
g.drawLine(305, 3, 305, 305);
g.drawLine(3, 305, 305, 305);
g.setColor(Color.PINK);

for (int i = 0; i < listBody.size(); i++) {
g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9);
}
g.fillRect(x, y, 9, 9);
g.setColor(Color.ORANGE);
g.fillRect(fx, fy, 9, 9);

g.setColor(Color.DARK_GRAY);
str1 = "现在的长度为:" + snakeBody;
g.drawString(str, 10, 320);
g.drawString(str1, 10, 335);
}

public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
if (makeOut() == false) {
JOptionPane.showMessageDialog(null, "重新开始......");

speed = 700;

snakeBody = 1;

x = 5;
y = 5;

list.clear();
list.add(new Point(x, y));
listBody.clear();
listBody.add(list.get(0));

dx = 10;
dy = 0;

}
addPoint(x, y);
if (x == fx && y == fy) {
speed = (int) (speed * 0.8);//速度增加参数
if (speed < 200) {
speed = 100;
}
fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2
snakeBody++;// 2
} // 2
repaint();
}

public void addPoint(int xx, int yy) {
// 动态的记录最新发生的50步以内的移动过的坐标
// 并画出最新的snakeBody
if (list.size() < 100) {//蛇身长度最长为100
list.add(new Point(xx, yy));
} else {
list.remove(0);
list.add(new Point(xx, yy));
}
if (snakeBody == 1) {
listBody.remove(0);
listBody.add(0, list.get(list.size() - 1));
} else {
listBody.clear();
if (list.size() < snakeBody) {
for (int i = list.size() - 1; i > 0; i--) {
listBody.add(list.get(i));
}
} else {
for (int i = list.size() - 1; listBody.size() < snakeBody; i--) {
listBody.add(list.get(i));
}
}
}
}

public boolean makeOut() {
if ((x < 3 || y < 3) || (x > 305 || y > 305)) {
return false;
}
for (int i = 0; i < listBody.size() - 1; i++) {
for (int j = i + 1; j < listBody.size(); j++) {
if (listBody.get(i).equals(listBody.get(j))) {
return false;
}
}
}
return true;
}
}

/*贪吃蛇代码*/

❸ 如何快速读懂项目源码javaWeb

一:学会如何读一个JavaWeb项目源代码 步骤:表结构->web.xml->mvc->db->spring
ioc->log-> 代码
1、先了解项目数据库的表结构,这个方面是最容易忘记 的,有时候我们只顾着看每一个方法是怎么进行的,却没
有去了解数据库之间的主外键关联。其实如果先了解数据 库表结构,再去看一个方法的实现会更加容易。
2、然后需要过一遍web.xml,知道项目中用到了什么拦
截器,监听器,过滤器,拥有哪些配置文件。如果是拦截 器,一般负责过滤请求,进行AOP 等;如果是监 可能是定时任务,初始化任务;配置文件有如使用了 spring
后的读取mvc 相关,db 相关,service 相关,aop 相关的文件。
3、查看拦截器,监听器代码,知道拦截了什么请求,这
个类完成了怎样的工作。有的人就是因为缺少了这一步, 自己写了一个action,配置文件也没有写错,但是却怎么
调试也无法进入这个action,直到别人告诉他,请求被拦
4、接下来,看配置文件,首先一定是mvc相关的,如 springmvc
中,要请求哪些请求是静态资源,使用了哪些 view 策略,controller 注解放在哪个包下等。 然后是db 相关配置文件,看使用了什么数据库,使用了
什么orm框架,是否开启了二级缓存,使用哪种产品作 为二级缓存,事务管理的处理,需要扫描的实体类放在什 么位置。最后是spring 核心的ioc
功能相关的配置文件, 知道接口与具体类的注入大致是怎样的。当然还有一些如 apectj 置文件,也是在这个步骤中完成
5、log
相关文件,日志的各个级别是如何处理的,在哪些 地方使用了log 记录日志
6、从上面几点后知道了整个开源项目的整体框架,阅读 每个方法就不再那么难了。
7、当然如果有项目配套的开发文档也是要阅读的。

❹ 在网上搜了一些java web项目,运行的时候需要登录密码,密码在源码里吗

应该不是在源码里面吧...

有些登陆的随便输入密码就行了..如果不是的话应该会有说明的..

你再仔细看看...

阅读全文

与javaweb简单项目源码相关的资料

热点内容
iis如何安装php 浏览:791
k5嗜血魔键安卓怎么调好用 浏览:834
建行app中如何添加银行卡 浏览:281
简便算法100点 浏览:161
如何创新我的世界服务器 浏览:881
战地怎么看服务器地址 浏览:348
vue怎么打包放上服务器 浏览:165
为什么安卓服夏日活动没有兔子头 浏览:894
pubg为什么显示服务器连接失败 浏览:650
阿里云扫码登录服务器 浏览:971
化学基础pdf 浏览:896
51单片机晶码管 浏览:281
怎么查服务器假死原因日志在哪看 浏览:277
扫描pdf文件 浏览:926
解压密码百度云在线解压 浏览:767
传播学算法推荐 浏览:749
我的世界网络游戏如何查找服务器 浏览:258
安卓和苹果通讯录怎么互传 浏览:203
怎么打开隐私与应用加密的菜单 浏览:416
我的世界服务器小游戏的地址大全 浏览:578