jsp相关笔记(三)

时间:2023-02-14 04:59:52

在往数据库的表中写入数据时,一定要与表中的每一项对应,比如表中有三项,则写入的值也应该是三类:

 String getTitle=request.getParameter("title");
String getContent=request.getParameter("message");
java.text.SimpleDateFormat time = new java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
String sql = "INSERT INTO message(title, content, time) VALUES ('"+getTitle+"', '"+getContent+"', ' "+time.format(new java.util.Date())+"')";

在往数据库中要写入中文时,要注意所有的格式都应该是utf-8,此外还应在链接数据库时加入:

 String url="jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8";