App极限开发第二天

今天着重于制作帖子的原型,由于整个app的基础便是帖子,所以帖子的制作显得尤为重要

确认帖子在数据库中储存的格式:(暂定)

 

 然后是对帖子的编写:

package com.example.tmchat;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Message;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TieziActivity extends AppCompatActivity {

    private Connection conn;
    private PreparedStatement ps;
    private String tid;
    private String title;
    private String fenqu;
    private String username;
    private String date;
    private TextView tv_title;
    private TextView tv_username;
    private TextView tv_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tiezi);

        initUI();
        initData();
    }

    private void initData() {

        //获取传来的tid
        Bundle bundle=getIntent().getExtras();	//接收端(这里接收到的bundle为一捆)
        tid = bundle.getString("tid");


        //连接数据库
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    onmysql();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        }).start();


        System.out.println("输出");
        //为标题,发布者,发布时间设置文字
       // tv_title.setText(title);
       // tv_username.setText(username);
       // tv_date.setText(date);

    }

    private void initUI() {

        //标题,发布者,发布时间
        tv_title = findViewById(R.id.title);
        tv_username = findViewById(R.id.username);
        tv_date = findViewById(R.id.date);




    }
    private void onmysql() throws SQLException {

        String url = "jdbc:mysql://192.168.137.1:3306/tmchat";

        conn = null;
        ps = null;
        System.out.println("此处为1");
        try {
            System.out.println("此处为2");
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("此处为3");
            conn = (Connection)DriverManager.getConnection(url, "guest", "guest");
            System.out.println("此处为4");

        } catch (ClassNotFoundException e) {
            System.out.print("加载驱动失败");
        } catch (SQLException e) {
            System.out.print("连接数据库失败");
        }

        System.out.println("此处为5");

        //是否查找到帖子
        boolean flags = false;

        try {
            ps = conn.prepareStatement("select * from tiezi");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                if(tid.equals(rs.getString("tid"))){
                        title = rs.getString("title");
                        fenqu = rs.getString("fenqu");
                        username = rs.getString("uername");
                        date = rs.getString("date");
                        break;
                }

            }
        }catch (SQLException e) {
            System.out.print("查找失败");
        }


    }



}

  

 

今日难点:  如何显示帖子的内容?

posted @ 2020-04-19 22:07  西西里啊  阅读(165)  评论(0编辑  收藏  举报