2014-08-29 58 views
-1

我使用microsoft sql server作爲數據庫並使客戶端服務器與服務器聊天server..when客戶端註冊並且密碼將散列並存儲在服務器數據庫中。 。但是,有63531錯誤讀取:「值」附近的語法不正確。我的數據庫用戶名的數據類型爲varchar和密碼類型爲nvarchar讓密碼散列爲utf8存入其當嘗試註冊用戶和密碼散列時,讀取「值」附近的錯誤語法錯誤

編碼數據庫連接和註冊

public class DBConnection { 
     java.sql.Connection conn = null; 


    public static java.sql.Connection connect(){ 
     try{ 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      java.sql.Connection conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;"+"user=looi963;password=1234;database=user"); 
      return conn; 
     } 
     catch(Exception e){ 
      JOptionPane.showMessageDialog(null,e); 
      return null; 
     } 

} 
    public void regis(String name,String password) throws SQLException 
    { 
     connect(); 
     PreparedStatement state=conn.prepareStatement("Insert into user where username=? and password=?"); 
     state.setString(1,name); 
     state.setString(2, md5(password)); 

     int count = state.executeUpdate(); 
     if(count>0) 
     { 
      System.out.println("aaddde"); 
     } 
    } 
    private String md5(String c) { 
     try { 
      MessageDigest digs = MessageDigest.getInstance("MD5"); 
      digs.update((new String(c)).getBytes("UTF8")); 
      String str = new String(digs.digest()); 
      return str; 
     }catch (Exception ex){ 
      return ""; 
     } 

,這是服務器端的編碼

public void regis(String name,String password) throws SQLException 
    { 

    try { 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
    } catch (ClassNotFoundException ex) { 
     Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex); 
    } 
     java.sql.Connection conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;"+"user=looi963;password=1234;database=user"); 
     PreparedStatement state=conn.prepareStatement("Insert into [user] (username,password) value(?,?)"); 
     state.setString(1,name); 
     state.setString(2, md5(password)); 

     int count = state.executeUpdate(); 
     if(count>0) 
     { 
      System.out.println("aaddde"); 
     } 
    } 
+1

將'value'更改爲'values' – 2014-08-29 12:17:10

回答

0

此語句的正確語法爲:

INSERT INTO table_name(column1,column2,column3,...) VALUES (值1,值2,值3,...);

請注意S位於VALUES和VALUES處。