数据库使用连接池到eclipse再到安卓studio的完整流程(对象)

数据库中,新建一张表,并插入相应的数据
数据库的表
在eclipse中再建一个实体类
public class Game_news {
private String game_news_img;
private String game_news_title;
private String game_news_context;
public Game_news(){

}
@Override
public String toString() {
return “Game_news [game_news_img=” + game_news_img + “, game_news_title=”
+ game_news_title + “, game_news_context=” + game_news_context
+ “]”;
}
public Game_news(String game_news_img, String game_news_title,
String game_news_context) {
super();
this.game_news_img = game_news_img;
this.game_news_title = game_news_title;
this.game_news_context = game_news_context;
}
public String getGame_news_img() {
return game_news_img;
}
public void setGame_news_img(String game_news_img) {
this.game_news_img = game_news_img;
}
public String getGame_news_title() {
return game_news_title;
}
public void setGame_news_title(String game_news_title) {
this.game_news_title = game_news_title;
}
public String getGame_news_context() {
return game_news_context;
}
public void setGame_news_context(String game_news_context) {
this.game_news_context = game_news_context;
}

}
还要建一个dao层接口
import java.sql.SQLException;
import java.util.List;

import com.zjn.bean.Game_news;

public interface IGame_news_dao {
public Game_news game_news() throws SQLException;
}
再建实现层,实现其方法
import java.io.PrintWriter;
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.zjn.bean.Game_ad;
import com.zjn.dao.IGame_ad_dao;
import com.zjn.util.JdbcUtils;

public class Game_ad_dao implements IGame_ad_dao{

@Override
public List<Game_ad> game_ad() throws SQLException {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    // TODO Auto-generated method stub
    String sql="select * from game_ad";

List ad=new ArrayList();
conn=JdbcUtils.getConnection();
ps=conn.prepareStatement(sql);
//返回的结果
rs=ps.executeQuery();
//遍历rs的数据
while(rs.next()){
Game_ad gad=new Game_ad(rs.getString(1), rs.getString(2));
ad.add(gad);
}
//由于是查询全部,所以不用set
//使用完之后要关闭
close(conn,ps,rs);

    return ad;
}

//关闭的方法
private void close(Connection conn,PreparedStatement ps,ResultSet rs) throws SQLException {
// TODO Auto-generated method stub
//要判断是否为空且不能同时判断
JdbcUtils.releaseConnection(conn);

    if(ps!=null){
        ps.close();
}
    if(rs!=null){
        rs.close();
    }
}

}
最后在Servlet里用doget方式来传值
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zjn.bean.Game_ad;
import com.zjn.bean.Game_news;
import com.zjn.daoimp.Game_ad_dao;
import com.zjn.daoimp.Game_news_dao;

@WebServlet(“/ConactionSqlAn”)
public class ConactionSqlAn extends HttpServlet {
private static final long serialVersionUID = 1L;

public ConactionSqlAn() {
    super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter printWriter=response.getWriter();
    String flag=request.getParameter("flag");
    switch (flag) {
    case "getad":
        List<Game_ad> list=null;
        try {
            //game_ad方法
            list = new Game_ad_dao().game_ad();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Gson gson=new Gson();
        String result=gson.toJson(list);
        printWriter.write(result);
        //输出结果后要关闭
        printWriter.close();
        break;
    case "getnews":
        Game_news news=null;
        try {
            news=new Game_news_dao().game_news();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Gson gson1=new Gson();
        String result1=gson1.toJson(news);
        printWriter.write(result1);
        printWriter.close();
        System.out.println("ConactionSqlAn.doGet()"+result1);
        break;
    default:
        break;
    }
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}
注意,要统一字符集编码格式,如果再有乱码,就在util包(工具包)下新建一个文件MyFilter
(核心代码)
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
// 修改配置文件,解决乱码问题
//在server.xml中添加
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
response.setContentType(“text/html;charset=utf-8”);
// pass the request along the filter chain
chain.doFilter(request, response);
}
最后是studio客户端
也要建一个跟eclipse一样的类,就可以复制上面的。
再用get请求
public class Home_fragment_game extends Fragment {
Gson ngson;
RequestParams requestParams;
//找到需要更新的标题
SimpleDraweeView news_img;
TextView title;
TextView context;
Game_news news;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_gamehome, container, false);
button= (RadioButton) view.findViewById(R.id.rb_dangji);
//找到新闻的控件ID
news_img= (SimpleDraweeView) view.findViewById(R.id.game_news_img);
title= (TextView) view.findViewById(R.id.game_news_title);
context= (TextView) view.findViewById(R.id.game_news_context);
initData();
private void initData() {
list=new ArrayList<>();
gson=new Gson();
url= Game_ad_Application.getUrl();
requestParams=new RequestParams(url);
ConnectNews();
}
private void ConnectNews() {
requestParams.addQueryStringParameter(“flag”,”getnews”);
x.http().get(requestParams, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
ngson=new Gson();
news=gson.fromJson(result,Game_news.class);

                    Log.i("zjn", "onSuccess: "+result);

                    //分别把得到的gson解析的结果赋给控件

                    news_img.setImageURI(news.getGame_news_img());

                    title.setText(news.getGame_news_title());
                    context.setText(news.getGame_news_context());


                    //分别把数据分配到smart和text上



                }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    });
}

}
这是对象的请求,里面用了Gson,SimpleDraweeView,还有Xutils依赖
compile ‘org.xutils:xutils:3.3.36’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值