今天写了一堆代码(一堆BUG),明天来改
package com.example.stusign;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.service.autofill.OnClickAction;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.stusign.pojo.Sign;
import com.example.stusign.pojo.StuDay;
import com.example.stusign.utils.MysqlUtils;
import com.example.stusign.utils.SignUtils;
import com.example.stusign.utils.StuUtils;
import java.sql.Connection;
import java.time.LocalDate;
public class SignActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.everyday);
Button button=findViewById(R.id.add);
button.setOnClickListener(this);
button=findViewById(R.id.close);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.add:
EditText editText=findViewById(R.id.id);
int id=Integer.parseInt(editText.getText().toString());
editText=findViewById(R.id.keywords);
String keywords=editText.getText().toString();
editText=findViewById(R.id.summary);
String summary=editText.getText().toString();
String sqlpath=getFilesDir()+"/sign.db";
SignUtils signUtils=SignUtils.getSignUtils(this);
SQLiteDatabase sqLiteDatabase=openOrCreateDatabase(sqlpath, Context.MODE_PRIVATE,null);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("打卡结果");
if(signUtils.add(sqLiteDatabase,new Sign(id, LocalDate.now().toString(),keywords,summary))){
StuDay stuDay=MysqlUtils.getdays(id);
builder.setMessage("打卡成功\n已坚持天数:\n"+Integer.toString(stuDay.getDays())+"最长坚持天数:"+Integer.toString(stuDay.getLongdays()));
}
else{
builder.setMessage("打卡失败");
}
builder.setPositiveButton("确定",null);
sqLiteDatabase.close();
builder.show();
break;
case R.id.close:
finish();
break;
}
}
}
package com.example.stusign;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.stusign.utils.MysqlUtils;
import com.example.stusign.utils.SignUtils;
import com.example.stusign.utils.StuUtils;
import com.example.stusign.pojo.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = findViewById(R.id.b1);
b1.setOnClickListener(this);
Button b2 = findViewById(R.id.b2);
b2.setOnClickListener(this);
Button b3 = findViewById(R.id.b3);
b3.setOnClickListener(this);
SQLiteDatabase sqLiteDatabase1 = openOrCreateDatabase(getFilesDir() + "/stu.db", Context.MODE_PRIVATE, null);
SQLiteDatabase sqLiteDatabase2 = openOrCreateDatabase(getFilesDir() + "/sign.db", Context.MODE_PRIVATE, null);
StuUtils stuUtils = StuUtils.getStuUtils(this);
stuUtils.onCreate(sqLiteDatabase1);
Stu stu = stuUtils.getstu(sqLiteDatabase1);
SignUtils signUtils = SignUtils.getSignUtils(this);
signUtils.onCreate(sqLiteDatabase2);
Sign sign = signUtils.getsign(sqLiteDatabase2);
TextView textView=findViewById(R.id.test);
textView.setText(String.valueOf(MysqlUtils.getConnection()));
/* MysqlUtils.addstu(stu);
MysqlUtils.addsign(sign);
stuUtils.delete(sqLiteDatabase1);
signUtils.delete(sqLiteDatabase2);
sqLiteDatabase1.close();
sqLiteDatabase2.close();
if (sign != null) {
List<Sign> signs = MysqlUtils.select(sign.getId());
int res = 0;
int k = 0;
for (int i = 0; i < signs.size() - 1; i++) {
Sign st = signs.get(i);
Sign en = signs.get(i + 1);
try {
DateFormat dft = new SimpleDateFormat("yyyy-mm-dd");
Date start = dft.parse(st.getDate());
Date end = dft.parse(en.getDate());
if (((long) end.getTime() - (long) start.getTime()) / 24 / 3600 / 1000 == 1) {
k++;
} else {
res = Math.max(res, k);
k = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
}
MysqlUtils.addStuDay(new StuDay(sign.getId(), signs.size(), res));
MysqlUtils.update(new StuDay(sign.getId(), signs.size(), res));
}
MysqlUtils.close();*/
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.b1:
startActivity(new Intent(MainActivity.this,ResigerActivity.class));
break;
case R.id.b2:
startActivity(new Intent(MainActivity.this,SignActivity.class));
break;
case R.id.b3:
break;
}
}
}
package com.example.stusign.utils;
import android.database.Cursor;
import com.example.stusign.pojo.Sign;
import com.example.stusign.pojo.Stu;
import com.example.stusign.pojo.StuDay;
import java.sql.*;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;
public class MysqlUtils {
private static String driver;
private static String url;
private static String username;
private static String password;
private static Connection connection;
private static PreparedStatement preparedStatement;
// static {
// try {
// driver="com.mysql.cj.jdbc.Driver";
// url="jdbc:mysql://172.18.56.110:3306/exam";
// username="root";
// password="111111";
// Class.forName(driver);
// connection=DriverManager.getConnection(url,username,password);
// }
// catch (Exception e){
// e.printStackTrace();
// }
// }
public static Connection getConnection(){
try {
driver="com.mysql.cj.jdbc.Driver";
url="jdbc:mysql://172.18.56.110:3306/exam";
username="root";
password="111111";
Class.forName(driver);
connection=DriverManager.getConnection(url,username,password);
}
catch (Exception e){
e.printStackTrace();
}
return connection;
}
public static void addstu(Stu stu){
try {
if(stu!=null) {
preparedStatement = connection.prepareStatement("insert ignore into stu values(?,?,?,?)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
preparedStatement.setInt(0, stu.getId());
preparedStatement.setString(1, stu.getName());
preparedStatement.setString(2, stu.getPhone());
preparedStatement.setString(3, stu.getUnit());
preparedStatement.execute();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void addsign(Sign sign){
try {
if(sign!=null) {
preparedStatement = connection.prepareStatement("insert ignore into `sign` values(?,?,?,?)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
preparedStatement.setInt(0, sign.getId());
preparedStatement.setString(1, sign.getDate());
preparedStatement.setString(2, sign.getKeywords());
preparedStatement.setString(3, sign.getSummary());
preparedStatement.execute();
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
public static List<Sign> select(int id){
try {
preparedStatement=connection.prepareStatement("select * from `sign` where id=? order by date",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
preparedStatement.setInt(0,id);
ResultSet resultSet=preparedStatement.executeQuery();
List<Sign> signs=new ArrayList<>();
while(resultSet.next()){
signs.add(new Sign(resultSet.getInt("id"),resultSet.getString("date"), resultSet.getString("keywords"),resultSet.getString("summary")));
}
return signs;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static void update(StuDay stuDay){
try {
if(stuDay!=null) {
preparedStatement = connection.prepareStatement("update studay set days=?,longdays=? where id=?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
preparedStatement.setInt(2, stuDay.getId());
preparedStatement.setInt(0, stuDay.getDays());
preparedStatement.setInt(1, stuDay.getLongdays());
preparedStatement.execute();
}
}
catch (Exception e){
e.printStackTrace();
}
}
public static void addStuDay(StuDay stuDay){
try {
if(stuDay!=null) {
preparedStatement = connection.prepareStatement("insert ignore into studay values(?,?,?)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
preparedStatement.setInt(0, stuDay.getId());
preparedStatement.setInt(1, stuDay.getDays());
preparedStatement.setInt(2, stuDay.getLongdays());
preparedStatement.execute();
}
}
catch (Exception e){
e.printStackTrace();
}
}
public static StuDay getdays(int id){
try {
preparedStatement=connection.prepareStatement("select * from studay where id=?",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
preparedStatement.setInt(0,id);
ResultSet resultSet=preparedStatement.executeQuery();
resultSet.next();
return new StuDay(resultSet.getInt("id"),resultSet.getInt("days"),resultSet.getInt("longdays"));
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
public static void close(){
try {
connection.close();
preparedStatement.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
标签:总结,preparedStatement,sign,static,import,今日,com,id From: https://www.cnblogs.com/liyiyang/p/17146397.html