This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes newbie problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "newbie problem" Watch "newbie problem" New topic
Author

newbie problem

Paul Beeman
Greenhorn

Joined: Aug 19, 2004
Posts: 3
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>
-----------------------------------------------------------
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
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

Joined: Aug 19, 2004
Posts: 3
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 ]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
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
Martin Lira
Ranch Hand

Joined: May 26, 2004
Posts: 97
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 ]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: newbie problem
 
Similar Threads
validation problem using jsp,servlets,ms-access db
Connection reset by peer:JVM_recv in socket input stream read
JDBC Connection (or "I Am At My Wits End")
Database access with servlet
Connection with database Help