我正在尝试每当用户登录系统时,该特定的用户名和当前时间将被捕获并存储到 MySQL 数据库中。我的数据库表名称是 userName、systemLoginTime、systemLogoffTime。我已经尝试了很多次,但是当用户登录系统时,数据库中不受影响
This is my code java API:
import java.io.*;
import java.net.*;
public class Test
{
public static void main(String[] args) throws IOException {
String userName = "";
String systemLoginTime = "";
URL url = new URL("http://localhost:8080/insert/{userName}/{systemLoginTime}");
//Insert your JSON query request
String query = "{'userName':USERNAME,'systemLoginTime':'TODAY'}";
//It change the apostrophe char to double colon char, to form a correct JSON string
query=query.replace("'", "\"");
try{
System.getenv().get("USERNAME");
java.util.Date today = new java.util.Date();
return new java.sql.Timestamp(today.getTime());
//com.sun.security.auth.module.NTSystem().getName;
String username = System.getProperty("user.name");
System.out.println("username = " + username);
//make connections
URLConnection urlc = url.openConnection();
//It Content Type is so importan to support JSON call
urlc.setRequestProperty("Content-Type", "application/json");
printMessage("Your URL: " + url.toString());
//use post mode
urlc.setDoOutput(true);
urlc.setAllowUserInteraction(false);
//send query
PrintStream ps = new PrintStream(urlc.getOutputStream());
ps.print(query);
printMessage("Your Query: " + query);
ps.close();
//get result
BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String l = null;
while ((l=br.readLine())!=null) {
printMessage(l);
}
br.close();
} catch (Exception e){
printMessage("Error ocurred");
printMessage(e.toString());
}
}
private static void printMessage(String s){
System.out.println(s);
}
}
========================================
This is my Spring Boot Repository class
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class MyCopRepo
{
@Autowired
JdbcTemplate jdbt;
public String add(String userName, String systemLoginTime)
{
String userName1= userName;
String systemLoginTime1=systemLoginTime;
//String systemLogoffTime1= systemLogoffTime;
String query = "insert into mycopinsert(userName, systemLoginTime) values(?,?)";
int i = jdbt.update(query, userName1, systemLoginTime1);
if(i>0)
{
return "Inserted Successfully";
}
return "Not Inserted";
}
}
================================================
This is my Controller class:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyCopController
{
@Autowired
MyCopRepo ar;
@PostMapping("/insert/{userName}/{systemLoginTime}")
public String myCop(@PathVariable("userName") String userName, @PathVariable("systemLoginTime") String systemLoginTime)
{
return ar.add(userName, systemLoginTime);
}
}
最佳答案
您可以尝试这样的方法,而不是作为参数传递,只需在查询数据库之前获取登录时间。
public String add(String userName)
{
String userName1= userName;
String systemLoginTime=java.sql.Timestamp(new Date().getTime());
//String systemLogoffTime1= systemLogoffTime;
String query = "insert into mycopinsert(userName, systemLoginTime) values(?,?)";
int i = jdbt.update(query, userName1, systemLoginTime1);
if(i>0)
{
return "Inserted Successfully";
}
return "Not Inserted";
}
}
您还应该将代码封装在 main 中,创建对内容进行分组的方法(而不是在每个部分之前添加注释),这样更容易理解且易于调试。
关于java - 如何使用java API监控登录时间和注销时间并存储到mysql数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62472972/