小程序连接本地mysql 数据库,显示书的信息
1、index.wxml页面:
<view>
<view class="commen_title">
<text>全部书籍</text>
</view>
<!-- 需用array进行添加项 -->
<view class="single_goods" wx:for="{{list}}" wx:key="key1" >
<view class="single_image_wrap" >
<image src="{{item.image}}" class="single_goods_image"></image>
</view>
<view class="single_goods_infor" >
<navigator url="../spdetail/spdetail?xuhao={{item.xuhao}}">
<view class="single_goods_name">
<text>书名:{{item.bookname}}</text>
</view>
<view class="single_goods_address">
<text>总量:{{item.zongliang}}</text>
</view>
<view class="single_goods_address">
<text>余量:{{item.yuliang}}</text>
</view>
<view class="single_goods_introduction">
<text>类型:{{item.leixing}}</text>
</view>
</navigator>
</view>
</view>
</view>
2、index.wxss页面:
// pages/all/all.js
Page({
/**
* 页面的初始数据
*/
data: {
list: [],
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (e) {
var that = this;
wx.request({
url: 'http://localhost:8080/login/allInfo',
data: {
},
success(res) {
console.log(res);
that.setData({
list: res.data
})
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
后台请求处理:
public static ArrayList<BookInfo> searchbook() {
Connection conn = null;
Statement stmt = null;
ArrayList<BookInfo> rst = new ArrayList<BookInfo>();
try {
Class.forName(JDBC_DRIVER);
System.out.println("正在连接数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
StringBuffer sql = new StringBuffer("select book.xuhao,bookname,zongliang,yuliang,sj_number,leixing,image FROM book,book_image where book.xuhao=book_image.xuhao");
ResultSet rs = stmt.executeQuery(sql.toString());
while (rs.next()) {
BookInfo dto = new BookInfo();
dto.setXuhao(rs.getInt("xuhao"));
dto.setBookname(rs.getString("bookname"));
dto.setZongliang(rs.getInt("zongliang"));
dto.setYuliang(rs.getInt("yuliang"));
dto.setSj_number(rs.getString("sj_number"));
dto.setLeixing(rs.getString("leixing"));
dto.setImage(rs.getString("image"));
rst.add(dto);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
return rst;
}