Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

What does illegal escape character error means?

843810Oct 2 2002 — edited Oct 4 2002
I wrote the SQL statements to save tables in database to a text file but there's error which i do not understand. Here is my code:

import java.sql.*;

public class SaveData
{
private Connection transConn;
protected ResultSet C1rs1,C1rs2,C1rs3,C2rs1,C2rs2,C2rs3,C3rs1,C3rs2,C3rs3,C4rs1,C4rs2,C4rs3,C5rs1,C5rs2,C5rs3;
protected ResultSet L1rs1,L1rs2,L1rs3,L2rs1,L2rs2,L2rs3,L3rs1,L3rs2,L3rs3,L4rs1,L4rs2,L4rs3,L5rs1,L5rs2,L5rs3;
protected Statement C1s1,C1s2,C1s3,C2s1,C2s2,C2s3,C3s1,C3s2,C3s3,C4s1,C4s2,C4s3,C5s1,C5s2,C5s3;
protected Statement L1s1,L1s2,L1s3,L2s1,L2s2,L2s3,L3s1,L3s2,L3s3,L4s1,L4s2,L4s3,L5s1,L5s2,L5s3;
String url = "jdbc:oracle:thin:@na-ye2oobav1rvw:1521:orab";

public SaveData()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
transConn=DriverManager.getConnection(url,"system","manager");

}
catch(Exception ex)
{
System.out.println("Cannot get connection.");
System.exit(0);
}//end catch
}//end constructor

public void saveC1()
{
try
{
C1s1 = transConn.createStatement();
C1rs1 = C1s1.executeQuery("Spool c:\C1.txt");

C1s2 = transConn.createStatement();
C1rs2 = C1s2.executeQuery("Select * from C1");

C1s3 = transConn.createStatement();
C1rs3 = C1s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("C1 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveC2()
{
try
{
C2s1 = transConn.createStatement();
C2rs1 = C2s1.executeQuery("Spool c:\C2.txt");

C2s2 = transConn.createStatement();
C2rs2 = C2s2.executeQuery("Select * from C2");

C2s3 = transConn.createStatement();
C2rs3 = C2s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("C2 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveC3()
{
try
{
C3s1 = transConn.createStatement();
C3rs1 = C3s1.executeQuery("Spool c:\C3.txt");

C3s2 = transConn.createStatement();
C3rs2 = C3s2.executeQuery("Select * from C3");

C3s3 = transConn.createStatement();
C3rs3 = C3s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("C3 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveC4()
{
try
{
C4s1 = transConn.createStatement();
C4rs1 = C4s1.executeQuery("Spool c:\C4.txt");

C4s2 = transConn.createStatement();
C4rs2 = C4s2.executeQuery("Select * from C4");

C4s3 = transConn.createStatement();
C4rs3 = C4s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("C4 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveC5()
{
try
{
C5s1 = transConn.createStatement();
C5rs1 = C5s1.executeQuery("Spool c:\C5.txt");

C5s2 = transConn.createStatement();
C5rs2 = C5s2.executeQuery("Select * from C5");

C5s3 = transConn.createStatement();
C5rs3 = C5s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("C5 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL1()
{
try
{
L1s1 = transConn.createStatement();
L1rs1 = L1s1.executeQuery("Spool c:\L1.txt");

L1s2 = transConn.createStatement();
L1rs2 = L1s2.executeQuery("Select * from L1");

L1s3 = transConn.createStatement();
L1rs3 = L1s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("L1 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL2()
{
try
{
L2s1 = transConn.createStatement();
L2rs1 = L2s1.executeQuery("Spool c:\L2.txt");

L2s2 = transConn.createStatement();
L2rs2 = L2s2.executeQuery("Select * from L2");

L2s3 = transConn.createStatement();
L2rs3 = L2s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("L2 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL3()
{
try
{
L3s1 = transConn.createStatement();
L3rs1 = L3s1.executeQuery("Spool c:\L3.txt");

L3s2 = transConn.createStatement();
L3rs2 = L3s2.executeQuery("Select * from L3");

L3s3 = transConn.createStatement();
L3rs3 = L3s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("L3 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL4()
{
try
{
L4s1 = transConn.createStatement();
L4rs1 = L4s1.executeQuery("Spool c:\L4.txt");

L4s2 = transConn.createStatement();
L4rs2 = L4s2.executeQuery("Select * from L4");

L4s3 = transConn.createStatement();
L4rs3 = L4s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("L4 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

public void saveL5()
{
try
{
L5s1 = transConn.createStatement();
L5rs1 = L5s1.executeQuery("Spool c:\L5.txt");

L5s2 = transConn.createStatement();
L5rs2 = L5s2.executeQuery("Select * from L5");

L5s3 = transConn.createStatement();
L5rs3 = L5s3.executeQuery("Spool off");
}
catch(SQLException e1)
{
System.out.println("L5 Table not saved. ");
System.exit(0);
e1.printStackTrace();

}//end catch
}//end method

}//end class

It generated the following error:

C:\SaveData\SaveData.java:32: illegal escape character
C1rs1 = C1s1.executeQuery("Spool c:\C1.txt");
^
C:\SaveData\SaveData.java:54: illegal escape character
C2rs1 = C2s1.executeQuery("Spool c:\C2.txt");
^
C:\SaveData\SaveData.java:76: illegal escape character
C3rs1 = C3s1.executeQuery("Spool c:\C3.txt");
^
C:\SaveData\SaveData.java:98: illegal escape character
C4rs1 = C4s1.executeQuery("Spool c:\C4.txt");
^
C:\SaveData\SaveData.java:120: illegal escape character
C5rs1 = C5s1.executeQuery("Spool c:\C5.txt");
^
C:\SaveData\SaveData.java:142: illegal escape character
L1rs1 = L1s1.executeQuery("Spool c:\L1.txt");
^
C:\SaveData\SaveData.java:164: illegal escape character
L2rs1 = L2s1.executeQuery("Spool c:\L2.txt");
^
C:\SaveData\SaveData.java:186: illegal escape character
L3rs1 = L3s1.executeQuery("Spool c:\L3.txt");
^
C:\SaveData\SaveData.java:208: illegal escape character
L4rs1 = L4s1.executeQuery("Spool c:\L4.txt");
^
C:\SaveData\SaveData.java:230: illegal escape character
L5rs1 = L5s1.executeQuery("Spool c:\L5.txt");
^

Could it be that spool command is not a SQL statement? Can someone help me??

Comments

843810
The '\' character in Java 'escapes' the character that follows it. It is very often used to signify tab ('\t'), line feed ('\n') and carriage return ('\r') characters in a string.

If you want to represent '\' as a character in itself, you need to include it twice, i.e. '\\'.

In you code, you have a line:
C2rs1 = C2s1.executeQuery("Spool c:\C2.txt");
and similar. If you replace all single backslashes with double, you should be fine:
C2rs1 = C2s1.executeQuery("Spool c:\\C2.txt");
For your information as well: Although Windows uses '\' where UNIX and Linux use '/' for directory and file paths, Java will allow you to use either on either operating system so you could actually replace each '\' with a '/':
C2rs1 = C2s1.executeQuery("Spool c:/C2.txt"); 
Hope this rambling makes sense!

Good luck...

Chris.
843810
blur,

ChrisBoy hit it dead on. That's the problem. The answer to your other question is that "spool" is an Oracle extension to SQL, so it should work for you, but it won't work if you change to different database. Doesn't sound like a problem, but something you might want to be aware of.

Dave
Although Windows uses '\' where
UNIX and Linux use '/' for directory and file paths,
Java will allow you to use either on either operating
system so you could actually replace each '\' with a
'/':
C2rs1 = C2s1.executeQuery("Spool c:/C2.txt");
As stated that is incorrect. "Java" doesn't do anything. That particular driver might do something with it. And there might be some classes in the Java API which do this, but java itself does not handle that.
843810
Chris...thanks for your help. I tried both ways and it works. Now there's no compilation error but there is runtime error. The information was not save to a file when i run them. This is my driver:

public class SaveApp
{
public static void main(String [] args)
{
//SaveButton frame = new SaveButton("Saving Tables");
//frame.setVisible(true);
SaveData s = new SaveData();
s.saveC1();
//s.saveL1();
}
}

The result when i run the driver: C1 Table not saved. (It gives the SQLException result.)

Why is it so? If I use the same SQL statements and execute in the SQL Plus, it is able to save to a file. Could it be the conection to the database?
843810
Chris...thanks for your help. I tried both ways and it works. Now there's no compilation error but there is runtime error. The information was not save to a file when i run them. This is my driver:

public class SaveApp
{
public static void main(String [] args)
{
//SaveButton frame = new SaveButton("Saving Tables");
//frame.setVisible(true);
SaveData s = new SaveData();
s.saveC1();
//s.saveL1();
}
}

The result when i run the driver: C1 Table not saved. (It gives the SQLException result.)

Why is it so? If I use the same SQL statements and execute in the SQL Plus, it is able to save to a file. Could it be the conection to the database?
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 1 2002
Added on Oct 2 2002
5 comments
800 views