|
|
|
|
@ -1,3 +1,363 @@
|
|
|
|
|
package com.example.fruitandvegetableguide.data.dbhelper
|
|
|
|
|
|
|
|
|
|
// TODO 这里放数据库工具方法,这些方法可以去网上找现成的
|
|
|
|
|
import android.util.Log
|
|
|
|
|
import androidx.compose.runtime.rememberCompositionContext
|
|
|
|
|
import com.example.fruitandvegetableguide.R
|
|
|
|
|
import com.example.fruitandvegetableguide.data.Guide
|
|
|
|
|
import com.example.fruitandvegetableguide.data.Post
|
|
|
|
|
import java.sql.Connection
|
|
|
|
|
import java.sql.DriverManager
|
|
|
|
|
import java.sql.PreparedStatement
|
|
|
|
|
import java.sql.ResultSet
|
|
|
|
|
import java.sql.SQLException
|
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
|
object DbHelper {
|
|
|
|
|
//private const val DB_URL = "jdbc:sqlserver://rm-2ze1sd060o140gu067o.sqlserver.rds.aliyuncs.com:3433;databaseName=FruitandVegetableGuide"
|
|
|
|
|
private const val DB_URL = "jdbc:jtds:sqlserver://rm-2ze1sd060o140gu067o.sqlserver.rds.aliyuncs.com:3433/FruitandVegetableGuide"
|
|
|
|
|
private const val USER = "aruan"
|
|
|
|
|
private const val PASS = "WRLXwrlx465434"
|
|
|
|
|
|
|
|
|
|
fun connect(): Connection? {
|
|
|
|
|
var conn: Connection? = null
|
|
|
|
|
try {
|
|
|
|
|
Class.forName("net.sourceforge.jtds.jdbc.Driver")
|
|
|
|
|
conn = DriverManager.getConnection(DB_URL, USER, PASS)
|
|
|
|
|
} catch (e: SQLException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} catch (e: ClassNotFoundException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
}
|
|
|
|
|
return conn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun closeConnection(conn: Connection?) {
|
|
|
|
|
try {
|
|
|
|
|
conn?.close()
|
|
|
|
|
} catch (e: SQLException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun login(username: String, password: String): Int {
|
|
|
|
|
val conn = connect()
|
|
|
|
|
var userId = -1 // 默认为 -1,表示未找到用户或不匹配
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
try {
|
|
|
|
|
val selectQuery = "SELECT id, password FROM Account WHERE username = ?"
|
|
|
|
|
val preparedStatement = conn.prepareStatement(selectQuery)
|
|
|
|
|
preparedStatement.setString(1, username) // 使用用户名作为参数
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
if (resultSet.next()) {
|
|
|
|
|
val storedPassword = resultSet.getString("password")
|
|
|
|
|
val userIdFromDB = resultSet.getInt("id")
|
|
|
|
|
// 检查提供的密码与数据库中存储的密码是否匹配
|
|
|
|
|
if (storedPassword == password) {
|
|
|
|
|
userId = userIdFromDB // 密码匹配,将用户 ID 设置为查询到的 ID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
resultSet.close()
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
} catch (e: SQLException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} finally {
|
|
|
|
|
closeConnection(conn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return userId
|
|
|
|
|
}
|
|
|
|
|
fun register(username: String, password: String): Boolean {
|
|
|
|
|
val conn = connect()
|
|
|
|
|
var rowsAffected = 0
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
try {
|
|
|
|
|
val insertQuery = "INSERT INTO Account (username, password) VALUES (?, ?)"
|
|
|
|
|
val preparedStatement = conn.prepareStatement(insertQuery)
|
|
|
|
|
preparedStatement.setString(1, username)
|
|
|
|
|
preparedStatement.setString(2, password)
|
|
|
|
|
rowsAffected = preparedStatement.executeUpdate()
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
|
|
|
|
|
} catch (e: SQLException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} finally {
|
|
|
|
|
closeConnection(conn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rowsAffected > 0
|
|
|
|
|
}
|
|
|
|
|
//未测试
|
|
|
|
|
fun postListInit(): MutableList<Post> {
|
|
|
|
|
val conn = connect()
|
|
|
|
|
val posts = mutableListOf<Post>()
|
|
|
|
|
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
try {
|
|
|
|
|
val postQuery = "SELECT * FROM Post LIMIT 100" // 假设post表是你的帖子表
|
|
|
|
|
|
|
|
|
|
val preparedStatement = conn.prepareStatement(postQuery)
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
val id = resultSet.getInt("id")
|
|
|
|
|
val userId = resultSet.getInt("userId")
|
|
|
|
|
val title = resultSet.getString("title")
|
|
|
|
|
val labels = resultSet.getString("labels")
|
|
|
|
|
val content = resultSet.getString("content")
|
|
|
|
|
val post = Post(id, userId, title, labels = labels, content = content)
|
|
|
|
|
posts.add(post)
|
|
|
|
|
}
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
} catch (e: SQLException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} finally {
|
|
|
|
|
closeConnection(conn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
println("贴子初始化成功")
|
|
|
|
|
return posts
|
|
|
|
|
}
|
|
|
|
|
fun getPostByPostId(postId: Int): Post {
|
|
|
|
|
val conn = connect()
|
|
|
|
|
var post = Post(0, 0, "", labels = "", content = "")
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
try {
|
|
|
|
|
val postQuery = "select * from Post where id = ?"
|
|
|
|
|
val preparedStatement = conn.prepareStatement(postQuery)
|
|
|
|
|
preparedStatement.setInt(1, postId)
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
resultSet.next()
|
|
|
|
|
val id = resultSet.getInt("id")
|
|
|
|
|
val userId = resultSet.getInt("userId")
|
|
|
|
|
val title = resultSet.getString("title")
|
|
|
|
|
val labels = resultSet.getString("labels")
|
|
|
|
|
val content = resultSet.getString("content")
|
|
|
|
|
post = Post(id = id, userId = userId, title = title, labels = labels, content = content)
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
} catch (e: SQLException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} finally {
|
|
|
|
|
closeConnection(conn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return post
|
|
|
|
|
}
|
|
|
|
|
fun getGuideByKind(kind: String): List<Guide> {
|
|
|
|
|
var guides = mutableListOf<Guide>()
|
|
|
|
|
var coon = connect()
|
|
|
|
|
if(coon != null){
|
|
|
|
|
try{
|
|
|
|
|
val guideQuery = "select * from Guide where kind = ?"
|
|
|
|
|
val preparedStatement = coon.prepareStatement(guideQuery)
|
|
|
|
|
preparedStatement.setString(1, kind)
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
while(resultSet.next()) {
|
|
|
|
|
val id = resultSet.getInt("id")
|
|
|
|
|
val kind = resultSet.getString("kind")
|
|
|
|
|
val identify = resultSet.getString("identify")
|
|
|
|
|
val suitable = resultSet.getString("suitable")
|
|
|
|
|
val taboo = resultSet.getString("taboo")
|
|
|
|
|
val nutritiveValue = resultSet.getString("nutritiveValue")
|
|
|
|
|
val recommendedMenu = resultSet.getString("recommendedMenu")
|
|
|
|
|
val guide = Guide(id, kind = kind, identify = identify, suitable = suitable, taboo = taboo, nutritiveValue = nutritiveValue, recommendedMenu = recommendedMenu)
|
|
|
|
|
guides.add(guide)
|
|
|
|
|
}
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
}catch(e: SQLException){
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
}finally{
|
|
|
|
|
closeConnection(coon)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return guides
|
|
|
|
|
}
|
|
|
|
|
fun getPostByLabels(labels: String): List<Post> {
|
|
|
|
|
var posts = mutableListOf<Post>()
|
|
|
|
|
var coon = connect()
|
|
|
|
|
if(coon != null){
|
|
|
|
|
try{
|
|
|
|
|
val postQuery = "select * from Post where labels LIKE ?"
|
|
|
|
|
val preparedStatement = coon.prepareStatement(postQuery)
|
|
|
|
|
val temp = "%$labels%"
|
|
|
|
|
preparedStatement.setString(1, temp)
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
while(resultSet.next()) {
|
|
|
|
|
val id = resultSet.getInt("id")
|
|
|
|
|
val userid = resultSet.getInt("userid")
|
|
|
|
|
val title = resultSet.getString("title")
|
|
|
|
|
val labels = resultSet.getString("labels")
|
|
|
|
|
val content = resultSet.getString("content")
|
|
|
|
|
val post = Post(id, userid, title, labels = labels, content = content)
|
|
|
|
|
posts.add(post)
|
|
|
|
|
}
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
}catch(e: SQLException){
|
|
|
|
|
//e.printStackTrace()
|
|
|
|
|
}finally{
|
|
|
|
|
closeConnection(coon)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return posts
|
|
|
|
|
}
|
|
|
|
|
fun getGuideByGuideId(guideId: Int): Guide {
|
|
|
|
|
val conn = connect()
|
|
|
|
|
var guide = Guide(0, kind = "", identify = "", suitable = "", taboo = "", nutritiveValue = "", recommendedMenu = "")
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
val guideQuery = "select * from Guide where id = ?"
|
|
|
|
|
val preparedStatement = conn.prepareStatement(guideQuery)
|
|
|
|
|
preparedStatement.setInt(1, guideId)
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
resultSet.next()
|
|
|
|
|
val id = resultSet.getInt("id")
|
|
|
|
|
val kind = resultSet.getString("kind")
|
|
|
|
|
val identify = resultSet.getString("identify")
|
|
|
|
|
val suitable = resultSet.getString("suitable")
|
|
|
|
|
val taboo = resultSet.getString("taboo")
|
|
|
|
|
val nutritiveValue = resultSet.getString("nutritiveValue")
|
|
|
|
|
val recommendedMenu = resultSet.getString("recommendedMenu")
|
|
|
|
|
guide = Guide(id=guideId, kind = kind, identify = identify, suitable = suitable, taboo = taboo, nutritiveValue = nutritiveValue, recommendedMenu = recommendedMenu)
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
} catch (e: SQLException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} finally {
|
|
|
|
|
closeConnection(conn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return guide
|
|
|
|
|
}
|
|
|
|
|
//未测试
|
|
|
|
|
fun guideListInit(): List<Guide> {
|
|
|
|
|
val conn = connect()
|
|
|
|
|
val guides = mutableListOf<Guide>()
|
|
|
|
|
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
try {
|
|
|
|
|
val guideQuery = "SELECT * FROM Guide LIMIT 100" // 假设post表是你的帖子表
|
|
|
|
|
|
|
|
|
|
val preparedStatement = conn.prepareStatement(guideQuery)
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
while (resultSet.next()) {
|
|
|
|
|
val id = resultSet.getInt("id")
|
|
|
|
|
val kind = resultSet.getString("kind")
|
|
|
|
|
val identify = resultSet.getString("identify")
|
|
|
|
|
val suitable = resultSet.getString("suitable")
|
|
|
|
|
val taboo = resultSet.getString("taboo")
|
|
|
|
|
val nutritiveValue = resultSet.getString("nutritiveValue")
|
|
|
|
|
val recommendedMenu = resultSet.getString("recommendedMenu")
|
|
|
|
|
val guide = Guide(id, kind = kind, identify = identify, suitable = suitable, taboo = taboo, nutritiveValue = nutritiveValue, recommendedMenu = recommendedMenu)
|
|
|
|
|
guides.add(guide)
|
|
|
|
|
}
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
} catch (e: SQLException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} finally {
|
|
|
|
|
closeConnection(conn)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
println("guideListInit成功")
|
|
|
|
|
return guides
|
|
|
|
|
}
|
|
|
|
|
fun postInsert(userid: Int, title: String, imgId: Int = R.drawable.loading, labels: String, content: String) : Boolean{
|
|
|
|
|
val coon = connect()
|
|
|
|
|
var rowsAffected = 0
|
|
|
|
|
if(coon != null){
|
|
|
|
|
try{
|
|
|
|
|
val postInsert = "insert into Post (userId, title, imgId, labels, content) values (?, ?, ?, ?, ?)"
|
|
|
|
|
val preparedStatement = coon.prepareStatement(postInsert)
|
|
|
|
|
preparedStatement.setInt(1, userid);
|
|
|
|
|
preparedStatement.setString(2, title)
|
|
|
|
|
preparedStatement.setInt(3, imgId)
|
|
|
|
|
preparedStatement.setString(4, labels)
|
|
|
|
|
preparedStatement.setString(5, content)
|
|
|
|
|
rowsAffected = preparedStatement.executeUpdate()
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
}catch (e: SQLException){
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
}finally{
|
|
|
|
|
closeConnection(coon)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
println("postInsert成功")
|
|
|
|
|
return rowsAffected > 0
|
|
|
|
|
}
|
|
|
|
|
fun getUsernameByUserid(userid: Int): String {
|
|
|
|
|
val coon = connect()
|
|
|
|
|
var username = ""
|
|
|
|
|
if(coon != null){
|
|
|
|
|
try{
|
|
|
|
|
val accountQuery = "select * from Account where id = ?"
|
|
|
|
|
val preparedStatement = coon.prepareStatement(accountQuery)
|
|
|
|
|
preparedStatement.setInt(1, userid)
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
resultSet.next()
|
|
|
|
|
username = resultSet.getString("username")
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
}catch (e:SQLException){
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
}finally {
|
|
|
|
|
closeConnection(coon)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
println("getUsernameByUserid成功")
|
|
|
|
|
return username
|
|
|
|
|
}
|
|
|
|
|
fun getPostByUserid(userid: Int): List<Post> {
|
|
|
|
|
var posts = mutableListOf<Post>()
|
|
|
|
|
var coon = connect()
|
|
|
|
|
if(coon != null){
|
|
|
|
|
try{
|
|
|
|
|
val postQuery = "select * from Post where userID = ?"
|
|
|
|
|
val preparedStatement = coon.prepareStatement(postQuery)
|
|
|
|
|
preparedStatement.setInt(1, userid)
|
|
|
|
|
val resultSet = preparedStatement.executeQuery()
|
|
|
|
|
while(resultSet.next()) {
|
|
|
|
|
val id = resultSet.getInt("id")
|
|
|
|
|
val userid = resultSet.getInt("userid")
|
|
|
|
|
val title = resultSet.getString("title")
|
|
|
|
|
val labels = resultSet.getString("labels")
|
|
|
|
|
val content = resultSet.getString("content")
|
|
|
|
|
val post = Post(id, userid, title, labels = labels, content = content)
|
|
|
|
|
posts.add(post)
|
|
|
|
|
}
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
}catch(e: SQLException){
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
}finally{
|
|
|
|
|
closeConnection(coon)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return posts
|
|
|
|
|
}
|
|
|
|
|
//有问题
|
|
|
|
|
fun DeletePost(postToDeleteList: List<Post>): Boolean {
|
|
|
|
|
var coon = connect()
|
|
|
|
|
var rowsAffected = 0
|
|
|
|
|
if(coon != null){
|
|
|
|
|
try{
|
|
|
|
|
val postDelete = "delete from Post where id = ?"
|
|
|
|
|
val preparedStatement = coon.prepareStatement(postDelete)
|
|
|
|
|
for (post in postToDeleteList) {
|
|
|
|
|
preparedStatement.setInt(1, post.id)
|
|
|
|
|
val resultSet = preparedStatement.executeUpdate()
|
|
|
|
|
if(resultSet > 0) rowsAffected++
|
|
|
|
|
}
|
|
|
|
|
preparedStatement.close()
|
|
|
|
|
}catch(e: SQLException){
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
}finally{
|
|
|
|
|
closeConnection(coon)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(rowsAffected == postToDeleteList.size) {
|
|
|
|
|
println("DeletePost成功")
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
//println("DeletePost失败")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|