版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/85029080
程序运行截图如下:
逻辑:
Qt修改数据库,java通过Hibernate读取数据库,然后显示,前端通过计时器实现不点刷新就能获取数据!
Qt源码如下:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSqlDatabase>
QT_BEGIN_NAMESPACE
class QSqlQuery;
QT_END_NAMESPACE
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected slots:
void textChanged(QString text);
private:
Ui::Widget *ui;
QSqlDatabase m_db;
QSqlQuery *m_query;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QSqlQuery>
#include <QVariant>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("CSDN IT1995");
m_db = QSqlDatabase::addDatabase("QMYSQL");
m_db.setHostName("localhost");
m_db.setDatabaseName("candjavadb");
m_db.setUserName("root");
m_db.setPassword("root");
if(m_db.open()){
qDebug()<< "open success!";
}
else{
qDebug()<< "open fail";
}
m_query = new QSqlQuery(m_db);
connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::textChanged(QString text)
{
m_query->prepare("update test set data=? where id=?");
m_query->addBindValue(text);
m_query->addBindValue("0");
m_query->exec();
}
java相关代码:
先是前端:
db.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>数据库操作</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="jquery/jquery.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="jquery/jquery.timer.js"></script>
<script >
function trace( msg ){
try{
console.log(msg);
}
catch(err){ }
}
function onQuery()
{
jQuery.ajax({
method: "GET",
url: "servlet/WebDB",
success: function(data, textStatus, jqXHR)
{
$("#userCount").html(data);
},
error: function( jqXHR, textStatus, errorThrown){
trace( "error: " + errorThrown );
}
});
}
// 页面加载后的初始化工作
$(document).ready( function(){
var timerId = $.timer(1000, onQuery, false);
});
</script>
</head>
<body>
<div class="container">
<p> 区域负荷 <span id="userCount"> 0 </span> </p>
</div>
</body>
</body>
</html>
servlet/WebDB中只修改了doGet方法:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
float data = new RegionalLoad().count();
String replyText = String.valueOf(data);
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(replyText);
out.close();
}
RegionalLoad.java
package my;
import my.db.*;
import org.hibernate.Session;
public class RegionalLoad {
public float count(){
Session dbss = null;
float floatData = 0.0f;
try{
dbss = HibernateSessionFactory.getSession();
Integer rowID = new Integer(0);
Test row = (Test)dbss.get(Test.class, rowID);
if(row == null){
System.out.println("找不到记录!");
}
else{
floatData = row.getData();
}
}
catch(Exception e){
System.out.println("出错:" + e.getMessage());
e.printStackTrace();
}
finally{
if(dbss != null)
dbss.close();
}
return floatData;
}
}