日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java后台两个表关联查询_简单界面+JAVA后台+数据库实现页面对数据库的两张关联表操作...

發布時間:2024/8/1 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java后台两个表关联查询_简单界面+JAVA后台+数据库实现页面对数据库的两张关联表操作... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前幾天寫了簡單的從頁面對數據庫進行的操作,下面對該功能進行升級,操作兩張相關聯的表;上次是對新聞類型的修改,我在這里就不重復了,可以查看我之前的博客,

首先從頁面說起:

頁面部分:

頁面部分我用了10個JSP完成的如圖:

也就是比上次的多了幾個news后綴的幾根文件

1、添加新聞內容

addnews,代碼如下

request.setCharacterEncoding("UTF-8");

String title=request.getParameter("title");//獲取文本框輸入的值

String contain=request.getParameter("contain");//獲取文本框輸入的值

String autor=request.getParameter("autor");//獲取文本框輸入的值

String typeid=request.getParameter("typeid");//獲取文本框輸入的值

if(title!=null){

//新建構造函數里面的對象

news n=new news(title,contain,autor,Integer.parseInt(typeid));

//新建Servlet層對象

NewsServlet ns=new NewsServlet();

int a=ns.addnews(n);//注意里面的參數,是news里面的參數,傳news對象

if(a>0){

//添加成功跳到show界面

response.sendRedirect("shownews.jsp"); //添加成功轉到查看頁面

}else{

//添加失敗在本界面界面

response.sendRedirect("addnews.jsp"); //添加成功轉到查看頁面

}

}

%>

這是添加新聞內容界面

標題:

內容:

作者:

類型:

//servlet對象//遍歷尋找類型

Type_NewsServlet tn=new Type_NewsServlet();

List list=tn.selectall();//調用其中的查看全部的方法

for(int i=0;i

type_news tpn=list.get(i);

%>

}

%>

shownews.jsp(用來查看頁面)

代碼如下:

function delete_(id){

var f=confirm("是否確定刪除?");

if(f){

location.href="shownews.jsp?ids="+id;

}else{

alert("您取消刪除");

}

}

function update(id){

location.href="updatenews.jsp?ids="+id;

}

String id=request.getParameter("ids");

if(id!=null){

NewsServlet ns=new NewsServlet();

int a =ns.delete(Integer.parseInt(id)); // us.deleten(Integer.parseInt(id));

response.sendRedirect("shownews.jsp");

}

%>

這是展示界面

編號標題內容作者類型操作

//調用Servlet取到數據

NewsServlet ns=new NewsServlet();

List list =new ArrayList();

list=ns.selectall(null);

for(int i=0;i

news n=list.get(i);

%>

)" value="修改"/>

)" value="刪除"/>

}

%>

updatenews.jsp(更新)

代碼如下:

request.setCharacterEncoding("UTF-8");

String id=request.getParameter("ids");

NewsServlet tsl=new NewsServlet(); //導包

news ts=new news();//導包

String type= request.getParameter("type");

if(type!=null){

String id1=request.getParameter("id");

String title=request.getParameter("title");//從下面的輸入取值

String contain=request.getParameter("contain");

String autor=request.getParameter("autor");

ts.setId(Integer.parseInt(id1));//強轉

ts.setTitle(title);

ts.setContain(contain);

ts.setAutor(autor);

int a=tsl.update(ts);

response.sendRedirect("shownews.jsp");

}else{

if(id!=null){

ts=tsl.selectone(Integer.parseInt(id));

}

}

%>

修改新聞界面


標題:

內容:

作者:

接下來是后臺程序:

servlet層:

代碼如下:

public class NewsServlet {

//新建后面層的對象,方便調用方法

NewsService ns=new NewsServiceImp();

/**********添加新聞內容***************************************************/

public int addnews(news n){

int a=0;

a=ns.addnews(n);

return a;

}

/**********查看新聞內容***************************************************/

public List selectall(news n){

List list=ns.selectall(null);

return list;

}

/**********刪除新聞內容***************************************************/

public int delete(int id){

int a=0;

a=ns.delete(id);

return a;

}

/**********查找一個新聞內容***************************************************/

public news selectone(int id){

news nn=ns.selectone(id);

return nn;

}

/**********更新新聞內容***************************************************/

public int update(news s){

int a=0;

a=ns.update(s);

return a;

}

}

service層:

代碼如下:

public interface NewsService {

public int addnews(news n);

public List selectall(news n);

public int delete(int id);

public news selectone(int id);

public int update(news s);

}

ServiceImp程序:

public class NewsServiceImp implements NewsService{//接口

//新建后面層的對象,方便調用方法

NewsDao nd=new NewsDaoImp();

/**********添加新聞內容***************************************************/

public int addnews(news n) {

int a=0;

a=nd.addnews(n);

return a;

}

/**********查看新聞內容***************************************************/

public List selectall(news n) {

List list=nd.selectall(n);

return list;

}

/**********刪除新聞內容***************************************************/

public int delete(int id) {

int a=0;

a=nd.delete(id);

return a;

}

/**********查找一個新聞內容***************************************************/

public news selectone(int id){

news nn=nd.selectone(id);

return nn;

}

/**********更新新聞內容***************************************************/

public int update(news s) {

int a=0;

a=nd.update(s);

return a;

}

}

Dao層:

public interface NewsDao {

public int addnews(news n);

public List selectall(news n);

public int delete(int id);

public news selectone(int id);

public int update(news s);

}

DaoImp程序:

public class NewsDaoImp implements NewsDao{//接口

Connection conn=null;

ResultSet rs=null;

PreparedStatement ps=null;

/**********添加新聞內容***************************************************/

public int addnews(news n) {

int a=0;

//連接數據庫

try {

conn=shujuku.conn();

String sql="insert into news values(?,?,?,?)";

ps=conn.prepareStatement(sql);

ps.setString(1, n.getTitle());

ps.setString(2, n.getContain());

ps.setString(3, n.getAutor());

ps.setInt(4, n.getTypeid());

a=ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}

return a;

}

/**********查看新聞內容***************************************************/

public List selectall(news n) {

List list=new ArrayList();

try {

conn=shujuku.conn();

String sql="select n.id,n.title,n.contain,n.autor,ts.typename " +

"from news as n " +

"inner join types as ts " +

"on n.typeid=ts.id";

ps=conn.prepareStatement(sql);

rs=ps.executeQuery();

while(rs.next()){

//另一個構造函數

news ty=new news(rs.getInt("id"), rs.getString("title"),

rs.getString("contain"),rs.getString("autor"),rs.getString("typename"));

list.add(ty);

}

} catch (SQLException e) {

e.printStackTrace();

}

return list;

}

/**********刪除新聞內容***************************************************/

public int delete(int id) {

int a=0;

try {

conn=shujuku.conn();

String sql="delete from news where id="+id;

ps=conn.prepareStatement(sql);

a=ps.executeUpdate();

} catch (SQLException e) {

e.printStackTrace();

}

return a;

}

/**********查找一個新聞內容***************************************************/

public news selectone(int id) {

news nn=new news();//還要有一個空的構造方法3

try {

conn=shujuku.conn();

String sql="select*from news where id="+id;

ps=conn.prepareStatement(sql);

rs=ps.executeQuery();

if(rs.next()){

nn.setId(rs.getInt("id"));

nn.setTitle(rs.getString("title"));

nn.setContain(rs.getString("contain"));

nn.setAutor(rs.getString("title"));

nn.setTypeid(rs.getInt("typeid"));

}

} catch (SQLException e) {

e.printStackTrace();

}

return nn;

}

/**********更新新聞內容***************************************************/

public int update(news s) {

int a=0;

try {

conn=shujuku.conn();

String sql="update news set title=?,contain=?,autor=? where id=?";

ps=conn.prepareStatement(sql);

ps.setString(1, s.getTitle());

ps.setString(2, s.getContain());

ps.setString(3, s.getAutor());

ps.setInt(4, s.getId());

a=ps.executeUpdate();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return a;

}

}

數據庫的表:

主外鍵關系:

之后還有對這篇文章的改進加入了C標簽把頁面的腳本都去掉:http://blog..net/qq_34178998/article/details/77579408

代碼就這么多:最后來看下運行效果吧:

后期加功能再繼續更新。。。。。。

也歡迎轉載!!!也歡迎轉載!!!也歡迎轉載!!!

總結

以上是生活随笔為你收集整理的java后台两个表关联查询_简单界面+JAVA后台+数据库实现页面对数据库的两张关联表操作...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。