RE: Question Marks Replacing Hyphens When Retreiving Data

From: Carb Simien [MSFT] (CarbinoS_at_online.microsoft.com)
Date: 09/21/04

  • Next message: Anna: "RE: SQL Server SP3a via JDBC SP2"
    Date: Tue, 21 Sep 2004 00:52:10 GMT
    
    

    --------------------
    | From: rkennel@veng.com (Ryan)
    | Newsgroups: microsoft.public.sqlserver.jdbcdriver
    | Subject: Question Marks Replacing Hyphens When Retreiving Data
    | Date: 6 Sep 2004 19:07:17 -0700
    | Organization: http://groups.google.com
    | Lines: 13
    | Message-ID: <49359f28.0409061807.383eddf@posting.google.com>
    | NNTP-Posting-Host: 68.41.54.66
    | Content-Type: text/plain; charset=ISO-8859-1
    | Content-Transfer-Encoding: 8bit
    | X-Trace: posting.google.com 1094522837 24593 127.0.0.1 (7 Sep 2004
    02:07:17 GMT)
    | X-Complaints-To: groups-abuse@google.com
    | NNTP-Posting-Date: Tue, 7 Sep 2004 02:07:17 +0000 (UTC)
    | Path:
    cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.s
    ul.t-online.de!t-online.de!news.glorb.com!postnews2.google.com!not-for-mail
    | Xref: cpmsftngxa10.phx.gbl microsoft.public.sqlserver.jdbcdriver:6312
    | X-Tomcat-NG: microsoft.public.sqlserver.jdbcdriver
    |
    | I am retrieving data ntext/text data from SQL using the Microsoft JDBC
    | driver.
    |
    | String text = rs.getString(1);
    |
    | I am noticing that all of the hyphens are being replaced with question
    | marks. When I export this data from SQL Server to a flat text file
    | though the text appears correctly.
    |
    | Do I need to do something special when setting up the JDBC Connection
    | or call parameters?
    |
    | Is there something special that I need to do while inserting the text?
    |

    Hi Ryan,

    Can you use the T-SQL script and JDBC code below and see the same problem?

    T-SQL:

            USE jdbc
            GO

            CREATE TABLE foo(col1 text, col2 ntext)
            INSERT INTO foo VALUES('123-45-6789',
    '{00000535-0000-0010-8000-00AA006D2EA4}')
            SELECT * FROM foo

    Java code:

            public static void main(String[] args) throws Exception
            {
                    // Load the JDBC driver
                    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

                    // Connect to SQL Server
                    Connection conn = null;
                    String url =
    "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jdbc;";

                    // Process the command line input
                    if (args.length == 0)
                    {
                            // Connect with default id/password
                            conn = DriverManager.getConnection(url, "sa", "mdacsa");
                    }
                    else if (args.length == 2)
                    {
                            // Connect with user-provided username and password
                            String userName = args[0];
                            String password = args[1];
                            conn = DriverManager.getConnection(url, userName, password);
                    }
                    else
                    {
                            // Invalid command -> throw an exception
                            throw new Exception("\nUsage: java query [userName password]\n");
                    }

                    // Ad hoc query
                    String sql = "SELECT * FROM foo";
                    Statement stmt = conn.createStatement();

                    // Execute the same query with different parameter values
                    ResultSet rs = null;
                    
                    rs = stmt.executeQuery(sql);
                    displayRows(rs);
                    rs.close();

                    // Cleanup
                    rs.close(); rs = null;
                    stmt.close(); stmt = null;
                    conn.close(); conn = null;
            }
            public static void displayRows(ResultSet rs)
            {
                    try
                    {
                            
    System.out.println("========================================================
    =========");
                            ResultSetMetaData rsmd = rs.getMetaData();
                            int numOfColumns = rsmd.getColumnCount();
                            int r = 0;

                            for(int i=1; i <= numOfColumns; i++ )
                            {
                                    System.out.print(rsmd.getColumnName(i));
                                    if(i != numOfColumns)
                                            System.out.print(" , ");
                            }
                            System.out.println("");
                            while(rs.next())
                            {
                                    r++;
                                    System.out.print("Row: " + r + ": ");
                                    for(int i=1; i <= numOfColumns; i++ )
                                    {
                                            System.out.print(rs.getString(i));
                                            if(i != numOfColumns)
                                                    System.out.print(" , ");
                                    }
                                    System.out.println("");
                            }
                    }
                    catch (SQLException e)
                    {
                            e.printStackTrace();
                    }
            }

    Carb Simien, MCSE MCDBA MCAD
    Microsoft Developer Support - Web Data

    Please reply only to the newsgroups.
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Are you secure? For information about the Strategic Technology Protection
    Program and to order your FREE Security Tool Kit, please visit
    http://www.microsoft.com/security.


  • Next message: Anna: "RE: SQL Server SP3a via JDBC SP2"
    Loading