10天冲刺第十天
今天做了:
界面不展示了,代码:
package com.example.psychological; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.psychological.javaClass.Dao; import com.example.psychological.javaClass.Essay; import com.example.psychological.javaClass.Record; import java.sql.SQLException; public class dailyActivity extends AppCompatActivity implements View.OnClickListener { private LinearLayout ll_daily; private LinearLayout myLinear; private Button bt_add_record; private Record[] records=new Record[100]; private Handler handler=new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message msg) { switch (msg.what){ case 0: int i=0; while (records[i].getId()!=null){ myLinear = (LinearLayout) LayoutInflater.from(dailyActivity.this).inflate(R.layout.record,null); TextView tv_title=myLinear.findViewById(R.id.tv_title); TextView tv_date=myLinear.findViewById(R.id.tv_date); tv_title.setText(records[i].getTitle()); tv_date.setText(records[i].getDate()); int finalI = i; myLinear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(dailyActivity.this, showRecordActivity.class); Bundle bundle=new Bundle(); bundle.putString("num",records[finalI].getNum()); intent.putExtras(bundle); startActivity(intent); } }); ll_daily.addView(myLinear); i++; } break; } return true; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_daily); getSupportActionBar().hide(); ll_daily=findViewById(R.id.ll_daily); bt_add_record=findViewById(R.id.bt_add_record); bt_add_record.setOnClickListener(this); initData(); } public void initData(){ new Thread(new Runnable() { @Override public void run() { Dao d=new Dao(); SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE); String id=sharedPreferences.getString("id",""); try { records=d.allRecord(id); } catch (SQLException e) { throw new RuntimeException(e); } Message msg=new Message(); msg.what=0; handler.sendMessage(msg); } }).start(); } @Override public void onClick(View v) { startActivity(new Intent(dailyActivity.this,recordActivity.class)); }
package com.example.psychological; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Looper; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.psychological.javaClass.Dao; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; public class recordActivity extends AppCompatActivity implements View.OnClickListener { private EditText ed_title; private EditText ed_context; private Button bt_commit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_record); getSupportActionBar().hide(); ed_title = findViewById(R.id.ed_title); ed_context = findViewById(R.id.ed_context); bt_commit = findViewById(R.id.bt_commit); bt_commit.setOnClickListener(this); } @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { Dao d= new Dao(); String title=ed_title.getText().toString(); String context=ed_context.getText().toString(); if(title.equals("")||context.equals("")){ Looper.prepare(); Toast.makeText(recordActivity.this,"输入为空", Toast.LENGTH_SHORT).show(); Looper.loop(); } SimpleDateFormat formatter= new SimpleDateFormat("yyyy.MM.dd"); Date date = new Date(System.currentTimeMillis()); SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE); String id=sharedPreferences.getString("id",""); try { String num= String.valueOf(d.getLength3(id)); d.addRecord(id,num,title,context,formatter.format(date)); } catch (SQLException e) { throw new RuntimeException(e); } finish(); startActivity(new Intent(recordActivity.this, dailyActivity.class)); } }).start(); } }
package com.example.psychological; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.example.psychological.javaClass.Dao; import com.example.psychological.javaClass.Essay; import com.example.psychological.javaClass.Record; import java.sql.SQLException; public class showRecordActivity extends AppCompatActivity { private TextView tv_title; private TextView tv_context; private Record record; private Handler handler=new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message msg) { switch (msg.what){ case 0: tv_title.setText(record.getTitle()); tv_context.setText(record.getContext()); break; } return true; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_show_record); getSupportActionBar().hide(); tv_title=findViewById(R.id.tv_title); tv_context=findViewById(R.id.tv_context); initData(); } public void initData(){ new Thread(new Runnable() { @Override public void run() { Dao d=new Dao(); Intent intent=getIntent(); String num=intent.getStringExtra("num"); SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE); String id=sharedPreferences.getString("id",""); Log.d("1111",id); Log.d("1111",num); try { record=d.checkRecord(id,num); } catch (SQLException e) { throw new RuntimeException(e); } Message msg=new Message(); msg.what=0; handler.sendMessage(msg); } }).start(); } }
包括上传记录,记录展示页面,二级页面
最后数据库工具类:
package com.example.psychological.javaClass; import android.util.Log; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Dao { private Connection conn = null; private Statement stmt; public int getLength() throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from user"; ResultSet rs= stmt.executeQuery(sql); int i=1; while (rs.next()){ i++; } stmt.close(); conn.close(); return i; } public int getLength2() throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from essay"; ResultSet rs= stmt.executeQuery(sql); int i=1; while (rs.next()){ i++; } stmt.close(); conn.close(); return i; } public int getLength3(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from record where id='"+id+"'"; ResultSet rs= stmt.executeQuery(sql); int i=1; while (rs.next()){ i++; } stmt.close(); conn.close(); return i; } public void addUser(User user) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="insert into user(id,name,sex,phnumber,password) values('"+user.getId()+"','"+user.getName()+"','"+user.getSex()+"','"+user.getPhnumber()+"','"+user.getPassword()+"')"; stmt.executeUpdate(sql); stmt.close(); conn.close(); } public void addNotice(String uid,String bid,String method,String date) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="insert into notice(uid,bid,method,date) values('"+uid+"','"+bid+"','"+method+"','"+date+"')"; stmt.executeUpdate(sql); stmt.close(); conn.close(); } public void addEssay(String num,String title,String context,String date) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="insert into essay(num,title,context,date) values('"+num+"','"+title+"','"+context+"','"+date+"')"; stmt.executeUpdate(sql); stmt.close(); conn.close(); } public void addRecord(String id,String num,String title,String context,String date) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="insert into record(id,num,title,context,date) values('"+id+"','"+num+"','"+title+"','"+context+"','"+date+"')"; stmt.executeUpdate(sql); stmt.close(); conn.close(); } public boolean havePhnumber(String phnumber) throws SQLException { boolean y=false; conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from user where phnumber = '"+phnumber+"'"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()){ y=true; } stmt.close(); conn.close(); return y; } public Boolean haveUser(String phnumber,String password) throws SQLException { boolean y=false; conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from user where phnumber = '"+phnumber+"' and password = '"+password+"'"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()){ y=true; } stmt.close(); conn.close(); return y; } public int haveCon(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql = "select * from consultant where id = '"+id+"'"; ResultSet rs = stmt.executeQuery(sql); int i = 0; String sta; if(rs.next()) { sta=rs.getString("state"); Log.d("111", sta); if(sta.equals("未审批")){ i=1; } else if(sta.equals("通过")) { i=2; } } stmt.close(); conn.close(); return i; } public User checkUser(String phnumber) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from user where phnumber = '"+phnumber+"'"; ResultSet rs = stmt.executeQuery(sql); User user=new User(); while (rs.next()){ user.setId(rs.getString("id")); user.setName(rs.getString("name")); user.setSex(rs.getString("sex")); user.setPhnumber(rs.getString("phnumber")); user.setPassword(rs.getString("password")); } stmt.close(); conn.close(); return user; } public User checkUser2(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from user where id = '"+id+"'"; ResultSet rs = stmt.executeQuery(sql); User user=new User(); while (rs.next()){ user.setId(rs.getString("id")); user.setName(rs.getString("name")); user.setSex(rs.getString("sex")); user.setPhnumber(rs.getString("phnumber")); user.setPassword(rs.getString("password")); } stmt.close(); conn.close(); return user; } public String checkName(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from user where id = '"+id+"'"; ResultSet rs = stmt.executeQuery(sql); String name=""; rs.next(); name=rs.getString("name"); stmt.close(); conn.close(); return name; } public Essay checkEssay(String num) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from essay where num = '"+num+"'"; ResultSet rs = stmt.executeQuery(sql); Essay essay=new Essay(); rs.next(); essay.setTitle(rs.getString("title")); essay.setContext(rs.getString("context")); stmt.close(); conn.close(); return essay; } public Record checkRecord(String id,String num) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from record where id = '"+id+"' and num = '"+num+"'"; ResultSet rs = stmt.executeQuery(sql); Record record=new Record(); rs.next(); record.setTitle(rs.getString("title")); record.setContext(rs.getString("context")); stmt.close(); conn.close(); return record; } public Consultant checkConsultant(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="select * from consultant where id = '"+id+"'"; ResultSet rs = stmt.executeQuery(sql); Consultant c = new Consultant(); while (rs.next()) { c.setId(rs.getString("id")); c.setName(rs.getString("name")); c.setIntroduction(rs.getString("introduction")); c.setMoney(rs.getString("money")); } stmt.close(); conn.close(); return c; } public void updateUser(User user) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="update user set name = '"+ user.getName() +"',sex = '"+user.getSex()+"',phnumber = '"+user.getPhnumber()+"' where id = '"+user.getId()+"'"; stmt.executeUpdate(sql); stmt.close(); conn.close(); } public void updateYes(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="update consultant set state = '通过' where id = '"+id+"'"; stmt.executeUpdate(sql); stmt.close(); conn.close(); } public void updateNo(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql="update consultant set state = '未通过' where id = '"+id+"'"; stmt.executeUpdate(sql); stmt.close(); conn.close(); } public Record[] allRecord(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); Record[] records=new Record[100]; for(int i=0;i<100;i++){ records[i]=new Record(); } String sql="select * from record where id = '"+id+"'"; ResultSet rs=stmt.executeQuery(sql); int i=0; while (rs.next()){ records[i].setId(rs.getString("id")); records[i].setNum(rs.getString("num")); records[i].setTitle(rs.getString("title")); records[i].setContext(rs.getString("context")); records[i].setDate(rs.getString("date")); i++; } stmt.close(); conn.close(); return records; } public Essay[] allEssay() throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); Essay[] essays=new Essay[100]; for(int i=0;i<100;i++){ essays[i]=new Essay(); } String sql="select * from essay"; ResultSet rs=stmt.executeQuery(sql); int i=0; while (rs.next()){ essays[i].setNum(rs.getString("num")); essays[i].setTitle(rs.getString("title")); essays[i].setContext(rs.getString("context")); essays[i].setDate(rs.getString("date")); i++; } stmt.close(); conn.close(); return essays; } public Notice[] allNotice(String id) throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); Notice[] notice = new Notice[100]; for (int i=0;i<100;i++){ notice[i]=new Notice(); } String sql="select * from notice where bid = '"+id+"'"; ResultSet rs= stmt.executeQuery(sql); int i=0; while (rs.next()){ notice[i].setUid(rs.getString("uid")); notice[i].setBid(rs.getString("bid")); notice[i].setMethod(rs.getString("method")); notice[i].setDate(rs.getString("date")); i++; } stmt.close(); conn.close(); return notice; } public Consultant[] allCon() throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql = "select * from consultant"; Consultant c[] = new Consultant[100]; for( int i=0;i<100;i++) { c[i] = new Consultant(); } ResultSet rs = stmt.executeQuery(sql); int i = 0; while (rs.next()) { if(rs.getString("state").equals("未审批")) { c[i].setId(rs.getString("id")); c[i].setName(rs.getString("name")); c[i].setMoney(rs.getString("money")); c[i].setIntroduction(rs.getString("introduction")); i++; } } stmt.close(); conn.close(); return c; } public Consultant[] allConyes() throws SQLException { conn = DBHelpOpen.getConn(); stmt = conn.createStatement(); String sql = "select * from consultant"; Consultant c[] = new Consultant[100]; for( int i=0;i<100;i++) { c[i] = new Consultant(); } ResultSet rs = stmt.executeQuery(sql); int i = 0; while (rs.next()) { if(rs.getString("state").equals("通过")) { c[i].setId(rs.getString("id")); c[i].setName(rs.getString("name")); c[i].setMoney(rs.getString("money")); c[i].setIntroduction(rs.getString("introduction")); i++; } } stmt.close(); conn.close(); return c; }
因为这个app功能都是我做的,一些小功能就不展示了,数据库中一共是创建了5个表