冲刺二

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

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

 

 然后是对帖子的编写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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 21:48  瞬沐队  阅读(98)  评论(0编辑  收藏  举报