[Java] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
เอา Code มาดูครับ
Date :
2013-02-08 06:42:23
By :
mr.win
Code (Java)
package jonaja;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import db.data;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class gogo extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField txtuser;
private JPasswordField txtpassword;
public static boolean LOGIN = false;
public static String username;
// public static String userlevel;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
gogo dialog = new gogo();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
protected void doLogin() {
try {
Connection conn = data.getConnection();
String sql = "SELECT * FROM login WHERE username = ? AND password = ? ";
PreparedStatement pre = conn.prepareStatement(sql);
pre.setString(1, txtuser.getText());
pre.setString(2, txtpassword.getText());
ResultSet rs = pre.executeQuery(sql);
if(rs.next()){
gogo.LOGIN = true;
gogo.username = txtuser.getText();
// gogo.userlevel = rs.getString("userlevel");
dispose();
}else{
JOptionPane.showMessageDialog(null,"username is wrong","Plese check your username",JOptionPane.ERROR_MESSAGE);
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"e","error",JOptionPane.ERROR_MESSAGE);
}
}
public gogo() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent arg0) {
if(!gogo.LOGIN){
System.exit(0);
}
}
});
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
txtuser = new JTextField();
txtuser.setBounds(190, 59, 86, 20);
contentPanel.add(txtuser);
txtuser.setColumns(10);
txtpassword = new JPasswordField();
txtpassword.setBounds(190, 104, 86, 20);
contentPanel.add(txtpassword);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
doLogin();
}
});
btnNewButton.setBounds(148, 176, 89, 23);
contentPanel.add(btnNewButton);
}
}
Date :
2013-02-08 17:44:27
By :
maclaren
Code (Android-Java)
PreparedStatement p = con.prepareStatement("select * from people where
(first_name = ? or last_name = ?) and address = ?");
p.setString(1, name);
p.setString(2, name);
p.setString(3, address);
รูปแบบนี้ก็น่าจะถูกแล้วน่ะครับ
Date :
2013-02-08 20:31:21
By :
mr.win
ไม่ได้อ่ะครับ
แต่codeนี้ผมไปทำเครื่องเพื่อนrunได้นะครับ
ไม่รู้เหมือนกันเป็นที่อะไร
เดี๋ยวว่าจะลองเอางานไปimportลงเครื่องเพื่อนละrunดู
เหนื่อยเลยเจอปัญหาที่ไม่รู้มาจากอะไร - -
Date :
2013-02-08 23:34:37
By :
maclaren
Load balance : Server 02