• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

newbie problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using a JSP page to connect to a Sybase DB using jConnect. It seems like I am able to connect to the DB but get a couple errors after connection. For some reason it works fine on my localhost using Netbeans but not on our Apache server. Here is my code below. I hope I put this in the correct forum.

-----------------------------------------------------------

<%@ page session="false" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.math.*" %>

<html>
<head><title>DB Connect</title></head>
<body>

<%

String DRIVER = "com.sybase.jdbc2.jdbc.SybDriver";
String URL = "jdbc:sybase:Tds:john.selco.info:2025/horizon?HOSTNAME=JavaProg";
String USER = "*********";
String PWD = "*********";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;

try {
//Step 1 - Load JDBC Driver
Class.forName(DRIVER);

//Step 2 - Create a Connection Object
conn = DriverManager.getConnection(URL,USER,PWD);
out.println("got connection<br>");

//Step 3 - Create a statement object and obtain a ResultSet object
stmt = conn.createStatement();
StringBuffer myquery = new StringBuffer();
myquery.append("select name, location");
myquery.append(" from location");
myquery.append(" order by name");
rs = stmt.executeQuery(myquery.toString());
}

catch(Exception e) {
System.out.println(e);
out.println("<b>"+"Error"+"</b>"+" "+e);
}

try {
// print the data
while(rs.next()) {
out.println(rs.getString(1)+ " " + rs.getString(2)+ "<br>");
} // end while
}

catch(Exception e) {
System.out.println(e);
out.println("<b>"+"Error"+"</b>"+" "+e);
}

finally {
try {
if(conn != null)
// conn.close();
if(stmt != null)
stmt.close();
if(rs != null)
rs.close();
} // end try
catch(Exception e) {
System.out.println(e);
out.println("<b>"+"Error"+"</b>"+" "+e);
} // end catch
} // end finally
%>

</body>
</html>
-----------------------------------------------------------
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Welcome to JavaRanch!

It would be really helpful if you could post the output from your program, i.e. what the errors are. That helps us to identify where any problems in the code are more easily. You may also find using e.printStackTrace() more illuminating than just e on its own.

Secondly, please use the UBB CODE tags when posting code as otherwise all your indenting disappears and it's very hard to read.

Having said all that one problem I did notice is that in your catch blocks, you're catching exceptions that should cause processing to finish at that point (e.g. failed to connect, so shouldn't go and try to run the query now...; query failed, best not to look at the non-existant record set then).

Anyway, that probably won't solve your problem, but if you post again we may get there more quickly.

A couple of tips: You'd benefit from reading up on Java style conventions (e.g. don't use all CAPS for variables, that's constants and it's rarely a good idea to catch Exception, catch more specific exceptions - SQLException in this case).

Jules
 
Paul Beeman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I am definitely a newbie.
-----------------------------------------------------
Error:
got connection
Error java.sql.SQLException: JZ006: Caught IOException: java.io.IOException: JZ0I6: An error occured converting UNICODE to the charset used by the server. Error message: java.io.CharConversionException: java.io.UnsupportedEncodingException: Cp850 Error java.lang.NullPointerException
-----------------------------------------------------


[edited password out]

[ August 19, 2004: Message edited by: Jeanne Boyarsky ]
[ August 20, 2004: Message edited by: Paul Beeman ]
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul, probably not a great plan to publish your sa password on a web forum. I'd edit that ASAP (replace with ****) using the little notepad/pencil icon at the top of the post if I were you.

Jules
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing that I noticed is you need to close the ResultSet before you close the Statement. The Statement.close() implicitly closes the ResultSet also. So in your case you will get an errror if the ResultSet.close() is executed.



As for your problem look at
this

[ August 19, 2004: Message edited by: Martin Lira ]

[ August 19, 2004: Message edited by: Martin Lira ]
[ August 19, 2004: Message edited by: Martin Lira ]
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

That's a very nasty problem you've got there - character set related. In case you can't decipher the conversation at the end of Martin's link and/or the error messages, the problem appears to be that your Sybase server is using a different character set to your client JVM, i.e. Cp850 (MSDOS Latin-1) and the JVM can't convert between the two. The linked thread is suggesting changing your default character set, but I don't know how to do that.

Anyway, as I can't help further I thought I'd give you some pointers as to how to lay out your code:

The comments saying which statements throw the exceptions are for your benefit only; that's not a convention. The stack trace will tell you exactly where in your code the exception occurs (class, method & line number). Notice that all processing is now in one try block so any checked exception will be caught and the finally block executed. No code we don't want to run will be reached afterwards.

Hope that's useful.

Jules
[ August 19, 2004: Message edited by: Julian Kennedy ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic