導航:首頁 > 源碼編譯 > 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簡單項目源碼相關的資料

熱點內容
桌面怎麼老是蹦出新建文件夾 瀏覽:705
阿里雲端伺服器怎麼下載 瀏覽:92
開發app哪裡最好用 瀏覽:526
安卓大屏導航怎麼裝頻譜軟體 瀏覽:753
魔百盒如何刪除自帶app 瀏覽:287
入住酒店哪個app首次優惠大 瀏覽:6
鑄鐵壓縮和扭轉的斷口破壞形式 瀏覽:234
單片機外文資料 瀏覽:117
myeclipsejava文件亂碼 瀏覽:750
魔獸世界安蘇伺服器為什麼叫貴族 瀏覽:747
程序員做火鍋視頻 瀏覽:288
ug數控車編程教程 瀏覽:693
鬥地主壓縮包 瀏覽:219
程序員走秀 瀏覽:942
阿里雲伺服器非五天無理由退款 瀏覽:192
pdf轉jpg工具的注冊碼 瀏覽:711
pdf保存列印 瀏覽:517
csgo社區伺服器怎麼顯示技術升級 瀏覽:432
程序員快餐模式教學 瀏覽:362
單片機pc介面 瀏覽:804