我在按赞按钮时尝试添加类似内容。我使用了一个简单的添加代码(我在下面粘贴它),但我一直收到错误:
Apr 06, 2018 10:30:08 AM Service.LikeService addlike
SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like (blog,user) values (1,'aaa')' at line 1
null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
nom
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at Service.LikeService.addlike(LikeService.java:146)
at Controller.CommentaireController.LikeAction(CommentaireController.java:231)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
我正在使用MVC架构,这是我的LikeService:
package Service;
import Model.like;
import Util.Connexion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LikeService {
private Connection con;
private Statement ste;
private PreparedStatement pst;
public LikeService() {
con=Connexion.getInstance().getConnection();
}
public void add1(like t, int id_pub) {
try{
//System.out.println(t.getCom_created());
Statement st = con.createStatement();
st.executeUpdate("insert into like(blog,user) values('"+id_pub+"','"+t.getUser()+"')");
}catch(SQLException ex){
System.out.println(ex);
}
}
/* public String afficher(int id_pub){
try{
pst = con.prepareStatement("select count(*) from jaime where id_pub = "+id_pub+" ");
ResultSet rs = pst.executeQuery();
int var=rs.getInt(1);
return Integer.toString(var);
}
catch (SQLException ex) {
Logger.getLogger(CommentaireDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return "0";
}*/
public int displayAllById(int i) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
List<Integer> list =new ArrayList();
try{
pst = con.prepareStatement("select * from like where blog="+i+" ");
ResultSet rs = pst.executeQuery();
while(rs.next()){
//créer une instance vide de personne()
list.add(rs.getInt(1));
//afficher l'instane
//System.out.println(l);
}
} catch (SQLException ex) {
Logger.getLogger(CommentaireService.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(list);
return list.size();
}
/*
public int liked(int i,String j) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
List<Integer> list =new ArrayList();
try{
pst = con.prepareStatement("select * from like where blog="+i+" and user="+j+" ");
ResultSet rs = pst.executeQuery();
while(rs.next()){
//créer une instance vide de personne()
list.add(rs.getInt(1));
//afficher l'instane
//System.out.println(l);
}
} catch (SQLException ex) {
Logger.getLogger(CommentaireService.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(list);
return list.size();
}*/
/*
public int plusjaime() {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
List<Integer> list =new ArrayList();
try{
pst = con.prepareStatement("select like.blog from like inner join forum on like.blog=forum.id where extract(month from forum.Cree)=extract(month from curdate()) group by id_pub limit 1");
ResultSet rs = pst.executeQuery();
while(rs.next()){
list.add(rs.getInt(1));
}
} catch (SQLException ex) {
Logger.getLogger(PublicationDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return list.get(0);
}
*/
public void addlike(like p) {
String rq = "insert into like (blog,user) values (?,?);";
try {
pst = con.prepareStatement(rq);
pst.setInt(1, p.getBlog());
pst.setString(2, p.getUser());
pst.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(LikeService.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/*
@Override
public void update(jaime t) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void delete(jaime t) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public List<jaime> displayAll() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public jaime findById(jaime t) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void add(jaime t) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
*/
我的控制员:
package Controller;
import static Controller.ForumController.a;
import static Controller.ForumController.cr;
import static Controller.ForumController.j;
import static Controller.ForumController.l;
import static Controller.ForumController.t;
import static Controller.LoginController.LoggedUser;
import Model.Commentaire;
import Model.Forum;
import Model.like;
import Service.CommentaireService;
import Service.ForumService;
import Service.LikeService;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* @author nessrine
*/
public class CommentaireController implements Initializable {
@FXML
private Label id;
@FXML
private ListView<String> listcom;
List< Commentaire> list2 = new ArrayList<Commentaire>();
@FXML
private JFXButton Ajouter1;
@FXML
private JFXButton Ajouter11;
@FXML
private JFXTextField commentaire;
like likee = new like();
@FXML
private JFXButton commenter;
Commentaire com=new Commentaire();
CommentaireService su=new CommentaireService();
LikeService li=new LikeService();
@FXML
private JFXButton modifier;
@FXML
private JFXButton supprimer;
public static int i;
public static String testpersonne;
List<String> listPersonne = new ArrayList< String>();
@FXML
private Label titre;
@FXML
private Label auteur;
@FXML
private Label date;
@FXML
private Label contenu;
@FXML
private Label tag;
@FXML
private JFXButton like;
@FXML
private Label likes;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
afficherID();
affichageInterface();
supprimer.setDisable(true);
modifier.setDisable(true);
/*int nbr = li.liked(j, LoggedUser.username) ;
if (nbr < 1) {
like.setDisable(false);
}
else
{
like.setDisable(true);
}*/
}
public boolean controleSaisie() {
if (commentaire.getText().isEmpty() ) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Required fields");
alert.setHeaderText(null);
alert.setContentText("Please fill in all fields");
alert.show();
return false;
}
return true;
}
public void afficherID()
{
id.setText(String.valueOf(l));
titre.setText(String.valueOf(t));
auteur.setText(String.valueOf(a));
contenu.setText(String.valueOf(cr));
tag.setText(String.valueOf(cr));
}
public void affichageInterface() {
CommentaireService su=new CommentaireService();
list2 = su.displayAllById(l);
ObservableList<String> itemss = FXCollections.observableArrayList();
for (int i = 0; i < list2.size(); i++) {
// System.out.println(list2.get(i).getContenu());
listPersonne.add(list2.get(i).getUser());
System.out.println(su.displayAllUser(list2.get(i).getUser()).getNom());
itemss.add("Le membre: "+ su.displayAllUser(list2.get(i).getUser()).getNom() + " " + su.displayAllUser(list2.get(i).getUser()).getPrenom() +" a commenté : "
+ list2.get(i).getCommentaire());
}
listcom.setItems(itemss);
}
@FXML
private void AjouterSujetLink(ActionEvent event) throws IOException {
Parent list_page_parent = FXMLLoader.load(getClass().getResource("/View/AddForum.fxml"));
Scene list_page_scene = new Scene(list_page_parent);
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.hide(); //optional
app_stage.setScene(list_page_scene);
app_stage.show();
}
@FXML
private void AfficherSujet(ActionEvent event) throws IOException {
Parent list_page_parent = FXMLLoader.load(getClass().getResource("/View/Forum.fxml"));
Scene list_page_scene = new Scene(list_page_parent);
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.hide(); //optional
app_stage.setScene(list_page_scene);
app_stage.show();
}
@FXML
private void commenter(ActionEvent event) {
if (controleSaisie()) {
com.setCommentaire(commentaire.getText());
com.setBlog(l);
com.setUser(LoggedUser.username);
su.add(com);
affichageInterface();
}
}
@FXML
private void modifier(ActionEvent event) {
su.update1(list2.get(i).getId(), commentaire.getText());
affichageInterface();
System.out.println("Modification avec succes ");
}
@FXML
private void supprimer(ActionEvent event) {
su.deletebyid(list2.get(i).getId());
affichageInterface();
System.out.println("Supprimer avec succes ");
}
@FXML
private void listCom_Clicked(MouseEvent event) {
i = listcom.getSelectionModel().getSelectedIndex();
testpersonne = listPersonne.get(i);
if (testpersonne.equals(LoggedUser.username) ) {
supprimer.setDisable(false);
} else {
supprimer.setDisable(true);
}
if (testpersonne.equals(LoggedUser.username)) {
modifier.setDisable(false);
} else {
modifier.setDisable(true);
}
commentaire.setText(list2.get(i).getCommentaire());
}
@FXML
private void LikeAction(ActionEvent event) {
like lik = new like(1, LoggedUser.username);
li.addlike(lik);
affichageInterface();
/* affichagelike();
System.out.println("ajouter");
int nbr = li.liked(1, LoggedUser.username) ;
if (nbr < 1) {
like.setDisable(false);
}
else
{
like.setDisable(true);
}*/
}
@FXML
private void affichagelike() {
int k;
k= li.displayAllById(j);
System.out.println(k);
likes.setText(Integer.toString(k) +" "+" j'aime");
}
}
这就是我的fxml和按钮的样子: