1 33 34 package com.internetcds.jdbc.tds; 35 36 import java.sql.*; 37 import java.math.BigDecimal ; 38 import java.util.StringTokenizer ; 39 import java.util.Vector ; 40 import java.util.Calendar ; 41 import java.io.*; 42 43 44 45 65 public class PreparedStatement_base 66 extends com.internetcds.jdbc.tds.Statement 67 implements PreparedStatementHelper 68 { 69 public static final String cvsVersion = "$Id: PreparedStatement_base.java,v 1.1 2006/06/23 10:39:30 sinisa Exp $"; 70 71 72 String rawQueryString = null; 73 Vector procedureCache = null; 74 ParameterListItem[] parameterList = null; 75 76 public PreparedStatement_base( 77 java.sql.Connection conn_, 78 Tds tds_, 79 String sql) 80 throws SQLException 81 { 82 super(conn_, tds_); 83 84 rawQueryString = sql; 85 86 int i; 87 int numberOfParameters = ParameterUtils.countParameters(rawQueryString); 88 89 parameterList = new ParameterListItem[numberOfParameters]; 90 for(i=0; i<numberOfParameters; i++) 91 { 92 parameterList[i] = new ParameterListItem(); 93 } 94 95 procedureCache = new Vector (); 96 } 97 98 99 protected void NotImplemented() throws java.sql.SQLException 100 { 101 throw new SQLException("Not Implemented"); 102 } 103 104 105 114 public void clearParameters() throws SQLException 115 { 116 int i; 117 for(i=0; i<parameterList.length; i++) 118 { 119 parameterList[i].clear(); 120 } 121 } 122 123 public void dropAllProcedures() 124 { 125 procedureCache = null; 126 procedureCache = new Vector (); 127 } 128 129 130 138 public boolean execute() throws SQLException 139 { 140 148 149 Procedure procedure = null; 150 boolean result = false; 151 152 closeResults(); 153 updateCount = -2; 154 155 ParameterUtils.verifyThatParametersAreSet(parameterList); 157 158 procedure = findCompatibleStoredProcedure(); 161 162 if (procedure == null) 165 { 166 167 procedure = new Procedure(rawQueryString, 169 tds.getUniqueProcedureName(), 170 parameterList, tds); 171 172 procedureCache.addElement(procedure); 174 175 submitProcedure(procedure); 177 } 178 result = executeCall(procedure.getProcedureName(), 179 procedure.getParameterList(), parameterList); 182 return result; 183 } 184 185 186 protected boolean executeCall( 187 String name, 188 ParameterListItem[] formalParameterList, 189 ParameterListItem[] actualParameterList) 190 throws SQLException 191 { 192 193 194 boolean result; 195 boolean wasCanceled = false; 196 197 try 198 { 199 SQLException exception = null; 200 PacketResult tmp = null; 201 202 203 tds.executeProcedure(name, 205 formalParameterList, 206 actualParameterList, 207 this, 208 timeout); 209 210 while (tds.isErrorPacket() || tds.isMessagePacket()) 211 { 212 tmp = tds.processSubPacket(); 213 exception = warningChain.addOrReturn((PacketMsgResult)tmp); 214 if (exception != null) 215 { 216 throw exception; 217 } 218 } 219 220 while(tds.isDoneInProc()) 221 { 222 tmp = tds.processSubPacket(); 223 } 224 225 if (tds.isProcId()) 226 { 227 tmp = tds.processSubPacket(); 228 } 229 230 231 if (tds.isResultSet()) 232 { 233 result = true; 234 } 235 else 236 { 237 result = false; 238 boolean done = false; 239 do 240 { 241 tmp = tds.processSubPacket(); 242 if (tmp instanceof PacketEndTokenResult) 243 { 244 done = ! ((PacketEndTokenResult)tmp).moreResults(); 245 wasCanceled = wasCanceled 246 || ((PacketEndTokenResult)tmp).wasCanceled(); 247 updateCount = ((PacketEndTokenResult)tmp).getRowCount(); 248 } 249 else if (tmp.getPacketType() 250 == TdsDefinitions.TDS_RET_STAT_TOKEN) 251 { 252 } 254 else 255 { 256 throw new SQLException("Protocol confusion" 257 + "Found a " 258 + tmp.getClass().getName() 259 + " (packet type 0x" 260 + Integer.toHexString(tmp.getPacketType() 261 & 0xff) 262 + ")"); 263 } 264 } while (!done); 265 } 266 } 267 catch(TdsException e) 268 { 269 e.printStackTrace(); 270 throw new SQLException(e.getMessage()); 271 } 272 catch(java.io.IOException e) 273 { 274 e.printStackTrace(); 275 throw new SQLException(e.getMessage()); 276 } 277 if (wasCanceled) 278 { 279 throw new SQLException("Query was canceled or timed out."); 280 } 281 282 return result; 283 } 284 285 286 287 private Procedure findCompatibleStoredProcedure() 288 throws SQLException 289 { 290 291 Procedure procedure = null; 292 int i; 293 294 for(i=0; i<procedureCache.size(); i++) 295 { 296 Procedure tmp = (Procedure)procedureCache.elementAt(i); 297 if (tmp.compatibleParameters(parameterList)) 298 { 299 procedure = tmp; 300 if (!tmp.hasLobParameters()) 301 { 302 break; 303 } 304 } 305 } 306 307 return procedure; 308 } 309 310 311 private void submitProcedure(Procedure proc) 312 throws SQLException 313 { 314 String sql = proc.getPreparedSqlString(); 315 tds.submitProcedure(sql, warningChain); 316 } 317 318 325 public java.sql.ResultSet executeQuery() throws SQLException 326 { 327 if (execute()) 328 { 329 startResultSet(); 330 } 331 else 332 { 333 throw new SQLException("Was expecting a result set"); 334 } 335 return results; 336 } 337 338 339 348 public int executeUpdate() throws SQLException 349 { 350 closeResults(); 351 352 if (execute()) 353 { 354 startResultSet(); 355 closeResults(); 356 throw new SQLException("executeUpdate can't return a result set"); 357 } 358 else 359 { 360 return getUpdateCount(); 361 } 362 } 363 364 365 381 public void setAsciiStream(int parameterIndex, 382 java.io.InputStream x, 383 int length) 384 throws SQLException 385 { 386 if (length == 0) { 388 setParam(parameterIndex, " ", 12, 1); 389 } 390 else { 391 byte[] b = new byte[length]; 392 try { 393 int i = x.read(b); 394 } catch (IOException ioe) { 395 } 396 setParam(parameterIndex, new String (b), 12, length); 397 } 398 } 399 400 401 410 public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException 411 { 412 setParam(parameterIndex, new Float (x.floatValue()), 7, -1); 414 } 415 416 417 432 public void setBinaryStream(int parameterIndex, 433 java.io.InputStream x, 434 int length) 435 throws SQLException 436 { 437 byte[] b = new byte[length]; 439 try { 440 int i = x.read(b); 441 } catch (IOException io) { 442 } 443 if (b == null || length <= 255) { 444 setParam(parameterIndex, b, -3, -1); 445 } 446 else { 447 setParam(parameterIndex, b, -4, -1); 448 } 449 } 450 451 452 460 public void setBoolean(int parameterIndex, boolean x) throws SQLException 461 { 462 byte[] b = new byte[1]; 463 464 if(x==true) { 465 b[0] = 1; 466 setParam(parameterIndex, new Byte ("1"), -7, -1); } 468 else { 469 b[0] = 0; 470 setParam(parameterIndex, new Byte ("0"), -7, -1); 472 } 473 474 } 475 476 477 485 public void setByte(int index, byte x) throws SQLException 486 { 487 byte[] b = new byte[1]; 489 b[0] = x; 490 setParam(index, b, -3, -1); 491 } 492 493 494 504 public void setBytes(int parameterIndex, byte x[]) throws SQLException 505 { 506 if (x == null || x.length<=255) 509 { 510 setParam(parameterIndex, x, java.sql.Types.VARBINARY, -1); 511 } 512 else 513 { 514 setParam(parameterIndex, x, java.sql.Types.LONGVARBINARY, -1); 515 } 516 } 517 518 519 527 public void setDate(int parameterIndex, java.sql.Date value) 528 throws SQLException 529 { 530 531 setParam(parameterIndex, 532 new java.sql.Date (value.getYear(), value.getMonth(), value.getDate()), 534 java.sql.Types.DATE, 535 -1); 536 } 537 538 539 547 public void setDouble(int parameterIndex, double value) throws SQLException 548 { 549 setParam(parameterIndex, new Double (value), java.sql.Types.DOUBLE, -1); 550 } 551 552 553 561 public void setFloat(int parameterIndex, float value) throws SQLException 562 { 563 setParam(parameterIndex, new Float (value), java.sql.Types.REAL, -1); 564 } 565 566 567 575 public void setInt(int index, int value) throws SQLException 576 { 577 setParam(index, new Integer (value), java.sql.Types.INTEGER, -1); 578 } 579 580 581 589 public void setLong(int parameterIndex, long value) throws SQLException 590 { 591 setParam(parameterIndex, new Long (value), java.sql.Types.BIGINT, -1); 592 } 593 594 595 604 public void setNull(int index, int type) throws SQLException 605 { 606 setParam(index, null, type, -1); 607 } 608 609 610 627 public void setObject(int parameterIndex, Object x) throws SQLException 628 { 629 String xname = x.getClass().getName(); 631 if (xname.equalsIgnoreCase("java.math.BigDecimal")) { 632 BigDecimal b = new BigDecimal (x.toString()); 633 Float f = new Float (b.floatValue()); 634 setParam(parameterIndex, new Float (b.floatValue()), 7, -1); 635 } 636 else if (xname.equalsIgnoreCase("java.lang.Boolean")) { 637 int[] i = new int[1]; 638 Boolean b = new Boolean (x.toString()); 639 if (b.equals("true")) { 640 i[0] = 1; 641 setParam(parameterIndex, i, -7, -1); 642 } 643 else { 644 i[0] = 1; 645 setParam(parameterIndex, i, -7, -1); 646 } 647 } 648 else if (xname.equalsIgnoreCase("java.lang.Byte")) { 649 Byte by = new Byte (x.toString()); 650 byte[] b = new byte[1]; 651 b[0] = by.byteValue(); 652 setParam(parameterIndex, b, -3, -1); 653 } 654 else if (xname.equalsIgnoreCase("java.sql.Date")) { 655 Date d = Date.valueOf(x.toString()); 656 setParam(parameterIndex, new Date(d.getYear(), d.getMonth(), d.getDate()), 657 91, -1); 658 } 659 else if (xname.equalsIgnoreCase("java.lang.Double")) { 660 setParam(parameterIndex, new Double (x.toString()), 8, -1); 661 } 662 else if (xname.equalsIgnoreCase("java.lang.Float")) { 663 setParam(parameterIndex, new Float (x.toString()), 6, -1); 664 } 665 else if (xname.equalsIgnoreCase("java.lang.Integer")) { 666 setParam(parameterIndex, new Integer (x.toString()), 4, -1); 667 } 668 else if (xname.equalsIgnoreCase("java.lang.Long")) { 669 setParam(parameterIndex, new Long (x.toString()), -5, -1); 670 } 671 else if (xname.equalsIgnoreCase("java.lang.Short")) { 672 setParam(parameterIndex, new Integer (x.toString()), 5, -1); 673 } 674 else if (xname.equalsIgnoreCase("java.lang.String")) { 675 setParam(parameterIndex, x.toString(), 12, x.toString().length()); 676 } 677 else if (xname.equalsIgnoreCase("java.sql.Time")) { 678 Time t = Time.valueOf(x.toString()); 679 setParam(parameterIndex, t, 92, -1); 680 } 681 else if (xname.equalsIgnoreCase("java.sql.Timestamp")) { 682 Timestamp ts = Timestamp.valueOf(x.toString()); 683 setParam(parameterIndex, ts, 93, -1); 684 } 685 else { 686 throw new SQLException("No validate Object type1."); 687 } 688 } 689 690 691 696 public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException 697 { 698 if (targetSqlType == 7) { 700 BigDecimal b = new BigDecimal (x.toString()); 701 Float f = new Float (b.floatValue()); 702 setParam(parameterIndex, new Float (b.floatValue()), 7, -1); 703 } 704 else if (targetSqlType == -7) { 705 int[] i = new int[1]; 706 Boolean b = new Boolean (x.toString()); 707 if (b.equals("true")) { 708 i[0] = 1; 709 setParam(parameterIndex, i, -7, -1); 710 } 711 else { 712 i[0] = 1; 713 setParam(parameterIndex, i, -7, -1); 714 } 715 } 716 else if (targetSqlType == -3) { 717 Byte by = new Byte (x.toString()); 718 byte[] b = new byte[1]; 719 b[0] = by.byteValue(); 720 setParam(parameterIndex, b, -3, -1); 721 } 722 else if (targetSqlType == 91) { 723 Date d = Date.valueOf(x.toString()); 724 setParam(parameterIndex, new Date(d.getYear(), d.getMonth(), d.getDate()), 725 91, -1); 726 } 727 else if (targetSqlType == 8) { 728 setParam(parameterIndex, new Double (x.toString()), 8, -1); 729 } 730 else if (targetSqlType == 6) { 731 setParam(parameterIndex, new Float (x.toString()), 6, -1); 732 } 733 else if (targetSqlType == 4) { 734 setParam(parameterIndex, new Integer (x.toString()), 4, -1); 735 } 736 else if (targetSqlType == -5) { 737 setParam(parameterIndex, new Long (x.toString()), -5, -1); 738 } 739 else if (targetSqlType == 5) { 740 setParam(parameterIndex, new Integer (x.toString()), 5, -1); 741 } 742 else if (targetSqlType == 12) { 743 setParam(parameterIndex, x.toString(), 12, x.toString().length()); 744 } 745 else if (targetSqlType == 92) { 746 Time t = Time.valueOf(x.toString()); 747 setParam(parameterIndex, t, 92, -1); 748 } 749 else if (targetSqlType == 93) { 750 Timestamp ts = Timestamp.valueOf(x.toString()); 751 setParam(parameterIndex, ts, 93, -1); 752 } 753 else { 754 throw new SQLException("No validate Object type2."); 755 } 756 } 757 758 765 private void setParam( 766 int index, 767 Object value, 768 int type, 769 int strLength) 770 throws SQLException 771 { 772 if (index < 1) 773 { 774 throw new SQLException("Invalid Parameter index " 775 + index + ". JDBC indexes start at 1."); 776 } 777 if (index > parameterList.length) 778 { 779 throw new SQLException("Invalid Parameter index " 780 + index + ". This statement only has " 781 + parameterList.length + " parameters"); 782 } 783 784 index--; 786 787 parameterList[index].type = type; 788 parameterList[index].isSet = true; 789 parameterList[index].value = value; 790 791 parameterList[index].maxLength = strLength; 792 } 794 795 798 820 public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) 821 throws SQLException 822 { 823 if (targetSqlType == 7) { 825 BigDecimal b = new BigDecimal (x.toString()); 826 b.setScale(scale); 827 Float f = new Float (b.toString()); 828 setParam(parameterIndex, new Float (b.floatValue()), 7, -1); 829 } 830 else if (targetSqlType == -7) { 831 int[] i = new int[1]; 832 Boolean b = new Boolean (x.toString()); 833 if (b.equals("true")) { 834 i[0] = 1; 835 setParam(parameterIndex, i, -7, -1); 836 } 837 else { 838 i[0] = 1; 839 setParam(parameterIndex, i, -7, -1); 840 } 841 } 842 else if (targetSqlType == -1) { 843 int len = x.toString().length(); 844 if (len==0) 845 { 846 setParam(parameterIndex, " ", java.sql.Types.VARCHAR, 1); 850 } 851 else 852 { 853 setParam(parameterIndex, x.toString(), java.sql.Types.VARCHAR, len); 854 } 855 } 856 else if (targetSqlType == -3) { 857 Byte by = new Byte (x.toString()); 858 byte[] b = new byte[1]; 859 b[0] = by.byteValue(); 860 setParam(parameterIndex, b, -3, -1); 861 } 862 else if (targetSqlType == 91) { 863 Date d = Date.valueOf(x.toString()); 864 setParam(parameterIndex, new Date(d.getYear(), d.getMonth(), d.getDate()), 865 91, -1); 866 } 867 else if (targetSqlType == 8) { 868 setParam(parameterIndex, new Double (x.toString()), 8, -1); 869 } 870 else if (targetSqlType == 6) { 871 setParam(parameterIndex, new Float (x.toString()), 6, -1); 872 } 873 else if (targetSqlType == 4) { 874 setParam(parameterIndex, new Integer (x.toString()), 4, -1); 875 } 876 else if (targetSqlType == -5) { 877 setParam(parameterIndex, new Long (x.toString()), -5, -1); 878 } 879 else if (targetSqlType == 5) { 880 setParam(parameterIndex, new Integer (x.toString()), 5, -1); 881 } 882 else if (targetSqlType == 12) { 883 setParam(parameterIndex, x.toString(), 12, x.toString().length()); 884 } 885 else if (targetSqlType == 92) { 886 Time t = Time.valueOf(x.toString()); 887 setParam(parameterIndex, t, 92, -1); 888 } 889 else if (targetSqlType == 93) { 890 Timestamp ts = Timestamp.valueOf(x.toString()); 891 setParam(parameterIndex, ts, 93, -1); 892 } 893 else { 894 throw new SQLException("No validate Object type3."+targetSqlType); 895 } 896 } 897 898 899 907 public void setShort(int index, short value) throws SQLException 908 { 909 setParam(index, new Integer (value), java.sql.Types.SMALLINT, -1); 910 } 911 912 913 923 public void setString(int index, String str) throws SQLException 924 { 925 int len = str.length(); 926 if (len==0) 927 { 928 setParam(index, " ", java.sql.Types.VARCHAR, 1); 932 } 933 else 934 { 935 setParam(index, str, java.sql.Types.VARCHAR, len); 936 } 937 } 938 939 940 948 public void setTime(int parameterIndex, java.sql.Time x) 949 throws SQLException 950 { 951 setParam(parameterIndex, new Time(x.getHours(), x.getMinutes(), x.getSeconds()), 953 92, -1); 954 } 955 956 957 966 public void setTimestamp(int index, java.sql.Timestamp value) 967 throws SQLException 968 { 969 setParam(index, value, java.sql.Types.TIMESTAMP, -1); 970 } 971 972 973 990 public void setUnicodeStream(int parameterIndex, java.io.InputStream x, int length) 991 throws SQLException 992 { 993 throw new SQLException("Not implemented"); 994 } 995 996 997 998 999 static public void main(String args[]) 1000 throws java.lang.ClassNotFoundException , 1001 java.lang.IllegalAccessException , 1002 java.lang.InstantiationException , 1003 SQLException 1004 { 1005 1006 java.sql.PreparedStatement stmt; 1007 String query = null; 1008 String url = url = "" 1009 + "jdbc:freetds:" 1010 + "//" 1011 + "kap" 1012 + "/" 1013 + "pubs"; 1014 1015 Class.forName("com.internetcds.jdbc.tds.Driver").newInstance(); 1016 java.sql.Connection connection; 1017 connection = DriverManager.getConnection(url, 1018 "testuser", 1019 "password"); 1020 1021 1022 stmt= connection.prepareStatement( 1023 "" 1024 +"select price, title_id, title, price*ytd_sales gross from titles" 1025 +" where title like ?"); 1026 stmt.setString(1, "The%"); 1027 java.sql.ResultSet rs = stmt.executeQuery(); 1028 1029 while(rs.next()) 1030 { 1031 float price = rs.getFloat("price"); 1032 if (rs.wasNull()) 1033 { 1034 System.out.println("price: null"); 1035 } 1036 else 1037 { 1038 System.out.println("price: " + price); 1039 } 1040 1041 String title_id = rs.getString("title_id"); 1042 String title = rs.getString("title"); 1043 float gross = rs.getFloat("gross"); 1044 1045 1046 System.out.println("id: " + title_id); 1047 System.out.println("name: " + title); 1048 System.out.println("gross: " + gross); 1049 System.out.println(""); 1050 } 1051 } 1052} 1053 | Popular Tags |