任何人都可以帮助我吗?当参数中没有空值时,为什么会收到此错误?
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 'null' at line 1
输入正常,代码接收它们,我唯一怀疑的是代码末尾的“VALUES”方法返回null。一点帮助PLZ?
String theuser = (String) sInput.readObject();
display(theuser);
String thepass = (String) sInput.readObject();
display(thepass);
String thename = (String) sInput.readObject();
display(thename);
String thephone = (String) sInput.readObject();
display(thephone);
String themail = (String) sInput.readObject();
display(themail);
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected
database...");
conn = DriverManager.getConnection(DB_URL, USER,
PASS);
System.out.println("Connected database
successfully...");
// create output first
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
// read the username
String theuser = (String) sInput.readObject();
display(theuser);
String thepass = (String) sInput.readObject();
display(thepass);
String thename = (String) sInput.readObject();
display(thename);
String thephone = (String) sInput.readObject();
display(thephone);
String themail = (String) sInput.readObject();
display(themail);
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected
database...");
conn = DriverManager.getConnection(DB_URL, USER,
PASS);
System.out.println("Connected database
successfully...");
//STEP 4: Execute a query
System.out.println("Inserting records into the
table...");
stmt = conn.createStatement();
String sql = "INSERT INTO ofuser " +
VALUES
(theuser,thepass,encrypted,thename,themail,4234,23432,thephone);
stmt.executeUpdate(sql);
// sql = "INSERT INTO Registration " +
// "VALUES (101, 'Mahnaz', 'Fatma', 25)";
// stmt.executeUpdate(sql);
// sql = "INSERT INTO Registration " +
// "VALUES (102, 'Zaid', 'Khan', 30)";
// stmt.executeUpdate(sql);
// sql = "INSERT INTO Registration " +
// "VALUES(103, 'Sumit', 'Mittal', 28)";
// stmt.executeUpdate(sql);
// System.out.println("Inserted records into the
table...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}
catch (IOException e) {
display("Exception creating new Input/output Streams: " +
e);
return;
}
// have to catch ClassNotFoundException
// but I read a String, I am sure it will work
catch (ClassNotFoundException e) {
}
}
// what will run forever
// remove myself from the arrayList containing the list of the
// connected Clients
public void main(String[] args) {
// start server on port 1500 unless a PortNumber is specified
int portNumber = 1500;
switch(args.length) {
case 1:
try {
portNumber = Integer.parseInt(args[0]);
}
catch(Exception e) {
System.out.println("Invalid port number.");
System.out.println("Usage is: > java Server
[portNumber]");
return;
}
case 0:
break;
default:
System.out.println("Usage is: > java Server
[portNumber]");
return;
}
// create a server object and start it
Server server = new Server(portNumber);
server.start();
}
// try to close everything
private void close() {
// try to close the connection
try {
if(sOutput != null) sOutput.close();
}
catch(Exception e) {}
try {
if(sInput != null) sInput.close();
}
catch(Exception e) {};
try {
if(socket != null) socket.close();
}
catch (Exception e) {}
}
/*
* Write a String to the Client output stream
*/
private boolean writeMsg(String msg) {
// if Client is still connected send the message to it
if(!socket.isConnected()) {
close();
return false;
}
// write the message to the stream
try {
sOutput.writeObject(msg);
}
// if an error occurs, do not abort just inform the user
catch(IOException e) {
display("Error sending message to " );
display(e.toString());
}
return true;
}
}
public String VALUES(String theuser, String thepass,String encrypted,
String thename, String themail, Object object2, Object object3,
String thephone) {
// TODO Auto-generated method stub
return null;
}
//public void main(String[] args) {
// System.out.println("Goodbye!");
// }//end main
答案 0 :(得分:1)
String sql = "INSERT INTO ofuser " +
VALUES
(theuser,thepass,encrypted,thename,themail,4234,23432,thephone);
上面的语法错误。它甚至不会编译。值应该用引号括起来。还要注意内部引用。
String sql = "INSERT INTO ofuser VALUES ('" + theuser + "','" + thepass + "','....)";
答案 1 :(得分:1)
您发送的SQL语句中存在问题 - 我假设其中一个值:
(theuser,thepass,加密,thename,themail,4234,23432,thephone)
设置为null。尝试将字符串打印到控制台,或通过调试器运行它以查看哪些变量为空。
希望有所帮助!