Java網頁應用之實現對資料庫的增、刪、改、查。
模擬資訊管理系統
通過eclipse實現網頁、資料庫的連線,實現對資料庫的增、刪、改、查。
登陸頁面:
登陸成功介面:
新增資訊介面:
修改資訊介面:
刪除資訊介面:
package com.lq.pro_user.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List ;
import com.lq.pro_user.domain.user;
import com.lq.pro_user.util.DBUlit;
//對資料庫進行操作
public class userDAO {
private ResultSet rs;
private PreparedStatement pst;
private Connection conn;
public List<user> querrAll(){ //在資料庫中取值,並返回。
List<user> ulist=new ArrayList<>(); //建立一個集合存放取出的值
try {
conn=DBUlit.opean(); //呼叫工具類中的opean()方法連線資料庫。
pst=conn.prepareStatement("select * from tuser");//宣告需要對資料庫進行的
rs=pst.executeQuery(); //從資料庫中取值。
while(rs.next()) {
user u=new user(); //呼叫user裡面的set方法給u賦值
u.setAddress(rs.getString("address"));
u.setAge(rs.getInt("age"));
u.setID(rs.getInt("iD"));
u.setName(rs.getString("name"));
ulist.add(u); //將從資料庫取到的值放入ulist中。
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUlit.close(rs, pst, conn); //呼叫工具類中的close()方法關閉資源。
}
return ulist; //將從資料庫中取到的值輸出。
}
public boolean add(user u) {
try {
conn=DBUlit.opean();
pst=conn.prepareStatement("insert into tuser values(?,?,?,?)");
pst.setInt(1, u.getID());
pst.setString(2, u.getName());
pst.setInt(3,u.getAge());
pst.setString(4, u.getAddress());
pst.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUlit.close(pst, conn);
}
return false;
}
public boolean del(int id) {
try {
conn=DBUlit.opean();
pst=conn.prepareStatement("delete tuser where id="+id);
pst.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUlit.close(pst, conn);
}
return false;
}
public user querryById(int id) {
try {
conn=DBUlit.opean();
pst=conn.prepareStatement("select * from tuser where id="+id);
rs=pst.executeQuery();
while(rs.next()) {
user u=new user();
u.setAddress(rs.getString("address"));
u.setAge(rs.getInt("age"));
u.setID(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUlit.close(rs, pst, conn);
}
return null;
}
public boolean update(user u) {
try {
conn=DBUlit.opean();
pst=conn.prepareStatement("update tuser set name=?,age=?,address=? where id="+u.getID());
pst.setString(1, u.getName());
pst.setInt(2, u.getAge());
pst.setString(3,u.getAddress());
pst.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUlit.close(pst, conn);
}
return false;
}
}
//user的get、set方法
package com.lq.pro_user.domain;
public class user {
private String name;
private String address;
private int ID;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.lq.pro_user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserLoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String name=req.getParameter("name");
String pass=req.getParameter("pass");
ServletConfig config=getServletConfig();
String uname=config.getInitParameter("username");
String upass=config.getInitParameter("userpass");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
if(uname.equals(name)&&upass.equals(pass)) {
resp.sendRedirect(req.getContextPath()+"/list");
}else {
resp.sendRedirect(req.getContextPath()+"/login_err.html");
}
}
}
package com.lq.pro_user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lq.pro_user.dao.userDAO;
import com.lq.pro_user.domain.user;
public class UserAddServlet extends HttpServlet {
private userDAO udao=new userDAO();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
user u=new user();
String id=req.getParameter("id");
String name=req.getParameter("name");
String age=req.getParameter("age");
String address=req.getParameter("address");
u.setAddress(address);
u.setAge(Integer.parseInt(age));
u.setID(Integer.parseInt(id));
u.setName(name);
boolean b=udao.add(u);
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
if (b) {
out.print("新增成功");
}else {
out.print("新增失敗");
}
}
}
package com.lq.pro_user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lq.pro_user.dao.userDAO;
import com.lq.pro_user.domain.user;
public class UserEditServlet extends HttpServlet {
private userDAO udao=new userDAO();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
user u=udao.querryById(Integer.parseInt(req.getParameter("id")));
if (u==null) {
out.print("查無此人");
}
out.print("<html>");
out.print("<head>");
out.print("</head>");
out.print("<body>");
out.print("<form action='update' method='post'>");
out.print("ID:<input type='text' value='"+u.getID()+"'readonly name='id'/><br>");
out.print("name:<input type='text' value='"+u.getName()+"'name='name'/><br>");
out.print("age:<input type='text' value='"+u.getAge()+"'name='age'/><br>");
out.print("address:<input type='text' value='"+u.getAddress()+"' name='address'/><br>");
out.print("<input type='submit' value='修改'/><br>");
out.print("</form>");
out.print("</body>");
out.print("</html>");
}
}
package com.lq.pro_user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lq.pro_user.dao.userDAO;
import com.lq.pro_user.domain.user;
public class UserUpdateServlet extends HttpServlet {
private userDAO udao=new userDAO();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String name= req.getParameter("name");
String id= req.getParameter("id");
String age= req.getParameter("age");
String address= req.getParameter("address");
user u=new user();
u.setID(Integer.parseInt(id));
u.setAddress(address);
u.setName(name);
u.setAge(Integer.parseInt(age));
boolean b=udao.update(u);
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
if (b) {
req.getRequestDispatcher("/list").forward(req, resp);
}
out.print("修改失敗");
}
}
package com.lq.pro_user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lq.pro_user.dao.userDAO;
import com.lq.pro_user.domain.user;
public class UserDelServlet extends HttpServlet {
private userDAO udao=new userDAO();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int id=Integer.parseInt(req.getParameter("id"));
user u =udao.querryById(id);
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
if (u==null) {
out.println("查無此人,刪除失敗");
}else {
boolean b=udao.del(id);
if(!b) {
out.print("刪除失敗");
}else {
req.getRequestDispatcher("/list").forward(req, resp);;
}
}
}
}
package com.lq.pro_user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lq.pro_user.dao.userDAO;
import com.lq.pro_user.domain.user;
public class UserDeleteServlet extends HttpServlet {
private userDAO udao=new userDAO();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int id=Integer.parseInt(req.getParameter("id"));
user u=udao.querryById(id);
resp.setContentType("text/html;charset=utf-8");
PrintWriter out=resp.getWriter();
if (u==null) {
out.print("所刪除的人資訊不存在!!");
return ;
}
out.print("<html>");
out.print("<head>");
out.print("</head>");
out.print("<body>");
out.print("<form action='del' method='post'>");
out.print("ID:<input type='text' value='"+u.getID()+"'readonly name='id'/><br>");
out.print("name:<input type='text' value='"+u.getName()+"'name='name'/><br>");
out.print("age:<input type='text' value='"+u.getAge()+"'name='age'/><br>");
out.print("address:<input type='text' value='"+u.getAddress()+"' name='address'/><br>");
out.print("<input type='submit' value='刪除'/><br>");
out.print("</form>");
out.print("</body>");
out.print("</html>");
}
}
package com.lq.pro_user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.jws.soap.SOAPBinding.Use;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.lq.pro_user.dao.userDAO;
import com.lq.pro_user.domain.user;
public class UserListServlet implements Servlet {
private userDAO dao=new userDAO();//建立userDAO的物件,用來呼叫userDAO中的方法,實現對user表的操作。
@Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
List<user> ulist=dao.querrAll();//將呼叫userDAO的方法返回的值放入ulist中,方便麵後面遍歷在網頁中顯示。
req.setAttribute("ulist",ulist);
req.getRequestDispatcher("/listView").forward(req, resp);
}
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
}
}
package com.lq.pro_user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.jni.User;
import com.lq.pro_user.domain.user;
public class UserListViewServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");//設定網頁的編碼格式。
List<user> ulist=(List<user>) req.getAttribute("ulist");
PrintWriter out=resp.getWriter(); //獲得輸出流
out.print("<html>"); //開始往網頁上輸出。
out.print("<head>");
out.print("</head>");
out.print("<body>");
out.print("<a href='add.html'>新增</a>");
out.print("<table with='80%' border='1'>");
out.print("<tr><td>ID</td><td>名字</td><td>年齡</td><td>地址</td><td>操作</td><td>操作</td></tr>");
for(user u:ulist) {
out.print("<tr><td>"+u.getID()+"</td><td>"+u.getName()+"</td><td>"+u.getAge()+"</td><td>"
+u.getAddress()+"</td><td><a href=edit?id="+u.getID()+">修改</a></td><td><a href=delete?id="+u.getID()+">刪除</a></td></tr>");
}
out.print("</table>");
out.print("</body>");
out.print("</html>");
}
}
package com.lq.pro_user.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.tomcat.jni.User;
public class DBUlit {
static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//載入驅動
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection opean() throws SQLException { //建立返回連線的開啟函式。
return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","9867");
}
public static void close(ResultSet rs,PreparedStatement pst,Connection conn) {//關閉資源
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst!=null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement pst,Connection conn) {
close(null, pst, conn);
}
}
配置xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name></display-name>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>list</servlet-name>
<servlet-class>com.lq.pro_user.servlet.UserListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>list</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>listview</servlet-name>
<servlet-class>com.lq.pro_user.servlet.UserListViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>listview</servlet-name>
<url-pattern>/listView</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.lq.pro_user.servlet.UserLoginServlet</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>userpass</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>add</servlet-name>
<servlet-class>com.lq.pro_user.servlet.UserAddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>add</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>edit</servlet-name>
<servlet-class>com.lq.pro_user.servlet.UserEditServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>edit</servlet-name>
<url-pattern>/edit</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>update</servlet-name>
<servlet-class>com.lq.pro_user.servlet.UserUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>update</servlet-name>
<url-pattern>/update</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>del</servlet-name>
<servlet-class>com.lq.pro_user.servlet.UserDelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>del</servlet-name>
<url-pattern>/del</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>com.lq.pro_user.servlet.UserDeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/delete</url-pattern>
</servlet-mapping>
</web-app>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
賬號:<input type="text" name="name"><br>
密碼:<input type="password"
相關推薦
Java網頁應用之實現對資料庫的增、刪、改、查。
模擬資訊管理系統
通過eclipse實現網頁、資料庫的連線,實現對資料庫的增、刪、改、查。
登陸頁面:
登陸成功介面:
新增資訊介面:
修改資訊介面:
刪除資訊介面:
package com.lq.pro_user.da
MySQL基礎操作增,刪,改,查。
一,SQL語言概述1,SQL語言是關係型資料庫的標準語言,用於維護管理資料庫,如資料查詢,資料更新,訪問控制,物件管理等功能。
2,建表
這是需要掌握的。(1)#Show databases; 檢視資料庫裡所有的小資料庫名。 show看 databases 資料庫小庫
(2)#Create databa
java連結mysql以及對資料庫的增,刪,改,查
package com.jdbcdemo;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStateme
mybatis的傳統的增,刪,改,查實現
3.2.1、儲存使用者
<insert id="saveUser" parameterType="com.atguigu.mybatis.pojo.User">
insert into t_user(`last_name`,`sex`) valu
Mapper介面方式的mybatis的增,刪,改,查實現
4.1、Mapper介面程式設計的命名習慣Mapper介面方式的程式設計,需要先有一個介面。這個介面的命名一般是xxxxMapper。
比如:
User模組的Mapper,介面命名為UserMapper。
Book模組的Mapper,介面命名為BookMappe
Java中List的增,刪,改,查
package Colection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
//備選課程類
public class L
laravel之seeder之操作(批量增,刪,改)
laravel對資料庫的表,進行批量的資料新增,修改,刪除,可使用seeder進行操作,步驟如下:
一, php artisan make:seeder 名字
二,在上面建立的seeder中寫操作程式碼(增,刪,改等)
三,在seeds/DataBaseS
shell指令碼操作mysql資料庫,使用mysql的-e引數可以執行各種sql的(建立,刪除,增,刪,改、查)等各種操作
來源:http://blog.163.com/xychenbaihu@yeah/blog/static/13222965520114116289991/
mysql -hhostname -Pport -uusername -ppassword -e 相關mysql的
dict的增,刪,改,查
date 關聯 print none 必須 數據類型 相同 一個 code dict是無序的 ,數據關聯性強,鍵值對,唯一一個個映射的數據類型 字典的鍵必須是可哈希的數據類型 (字符串,數字,bool,元祖)並且是唯一的. 不可哈希的:(列表,字典)
dict的增
1
Javascript操作Cookie(增,刪,改,查)
time() add return jna mts cap 關閉 nbsp expire
1 //1. 獲得指定名稱為ObjName的Cookie的值
2 function getCookie(ObjName){
3 var arrStr = documen
MySql cmd下的學習筆記 —— 有關表的操作(增,刪,改,查)
strong .com nsh utf str tab 主鍵 test har (知識回顧)
連接數據庫
mysql -uroot -p111
先建立一個新庫
create database test1;
use test1;
由於今天的主要內容是表的操作,建立表的詳細過
mysql基本功能 增,刪,改,查
增 ==》 insert into 庫.表(欄位名列表) values(值列表); mysql> create table meinvdb.mm(-> id int(4) not null primary key,-> name varchar(18) not null-> );Que
mysql基本功能 增,刪,改,查
cat dup pda key null cte insert value creat 增 ==》 insert into 庫.表(字段名列表) values(值列表); mysql> create table meinvdb.mm(-> id
python第四天課程:列表(增,刪,改,查),元祖
'''
#1.增加 append insert
li = ['alex',[1,2,3],'wusir','egon','女神','taibai']
li.append('日天')
print(li)
li.append(1)
print(li)
while 1:
name = input('請
asp.net core 2.1 dotnet(三)EF.core 的增,刪,改,查
1.呼叫方式:
增加引用:
using Microsoft.EntityFrameworkCore;
using Newtonsoft
ListView“表格編寫”與“增,刪,改”功能解析。
DataGridView控制元件功能十分強大,顯示錶格也方便,但效能就沒有那麼高;
ListView控制元件功能沒那麼強大,顯示錶格也要多寫程式碼,但效能相對高一些。
以下我總結出一系列對於ListView表格的詳細程式碼解析:
1.首先我們需要先建立一個
斷電導致oracle的ORA-01207錯誤完全解決辦法(重做日誌檔案(redo log files)管理(增,刪,改,查,切))
彙總整理一下有關重做日誌檔案(redo log files)管理相關的操作(增,刪,改,查,切)。供參考。1.當前日誌相關資訊[email protected]> select * from v$log; GROUP# THREAD# SEQUENCE# BYTES
GreenDao3.2使用詳解(增,刪,改,查,升級)
首先看一下效果圖:
專案結構如下圖所示:
第一步:在build中新增配置如下:
projet 目錄下的build.gradle
dependencies {
classpath 'org.greenrobot:greendao-gradl
表的完整語法增,刪,改,查( 子查詢 )
多行 教學 pan 比較 nbsp 匹配 字段 values 數據 表的完整語法增刪該查
表的完整 增 語法
1.所有數據按順序插入
insert [into] 表名 values (值1,……值n),[值1,……值n]
2.指定字段匹配插入,可以任意順序
SQL server中常見的增,刪,改,查的操作
--增--
/*增加列*/
alter table 表名
add 列名 varchar(20) check(你的約束條件);
如:在學生情況表中增加一個Email列,要求檢查輸入的是否為Email;
alter table 學生情況表
add Email varchar