- PR -

SQLServer:INSERT・UPDATEが実行できません。

1
投稿者投稿内容
まいこ
会議室デビュー日: 2005/07/01
投稿数: 9
投稿日時: 2005-07-01 22:30
*****  環境  *************************************************

・Windows2000(SP4)
・Tomcat5.0.28
・j2sdk1.4.2
・Eclipse2.1.3
・struts1.2.4
・SQLServer2000
・Microsoft SQL Server 2000 Driver for JDBC Ver2.2.0022


*****  状況  *************************************************

strutsのActionクラスより、SQLServer2000に接続をして、
DBのテーブルをSELECT、INSERT、UPDATEをする処理を行っています。

ところが、SELECTはうまくいくのですが、INSERT、UPDATEクエリを
executeUpdateするとSQLExceptionが発生してしまいます。

SQLServerのEnterprise Managerで対象ユーザの権限を見ても
SELECT・INSERT・UPDATE・DELETEにすべてチェックは入っています。
そして対象ユーザでログインしたクエリアナライザで
同じSQLを打っても、ちゃんと更新・挿入されます。

下にソース(DB・変数・テーブル名は実際のソース変えました。)と
結果を記載いたしますので、どなたかご教授いただけないでしょうか?


*****  ソース  *************************************************

[[[ Tomcatのconfのserver.xml ]]]

<Resource name="jdbc/sqlserver" auth="container"
type="javax.sql.DataSource" debug="1" />
<ResourceParams name="jdbc/sqlserver">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://・・・:1433;DatabaseName=mydb</value>
</parameter>
<parameter>
<name>username</name>
<value>myname</value>
</parameter>
<parameter>
<name>password</name>
<value>mypass</value>
</parameter>
<parameter>
<name>loginTimeout</name>
<value>3000</value>
</parameter>
</ResourceParams>


[[[ eclipseのprojectのweb.xml ]]]

<resource-ref>
<res-ref-name>jdbc/sqlserver</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


[[[ eclipseのprojectのcontext.xml ]]]

<Context path="/@appname@" docBase="@appname@">
<ResourceLink name="jdbc/sqlserver"
global="sqlserver"
type="javax.sql.DataSource" />
</Context>


[[[ eclipseのprojectのdatasource.properties ]]]
※ パッケージ = study.struts.model.biz

jndi.name = java:comp/env/jdbc/sqlserver


[[[ 処理記述 ]]]

Connection cone = null;
PreparedStatement stmt = null;
int ireccnt = 0;
String s_sql = "INSERT INTO TB1 VALUES('1111', '2');";

try{
cone = DataSourceFactory.getDataSource().getConnection();
cone.setAutoCommit(false);
stmt = cone.prepareStatement(s_sql);
ireccnt = stmt.executeUpdate(s_sql); ←ここで落ちます。
cone.commit();

}catch (SQLException sql_ex) {
System.out.println(sql_ex.getErrorCode());
System.out.println(sql_ex.getMessage());

}catch(Exception ex){
System.out.println(ex2.getMessage());

}
finally{
try{
if(stmt != null){
stmt.close();
}
if(cone != null){
cone.rollback();
cone.close();
}
}catch(SQLException sql_ex2){
System.out.println(sql_ex2.getErrorCode()); ←結果1
System.out.println(sql_ex2.getMessage()); ←結果2
}
}


*****  結果  *************************************************
結果1:0
結果2:[Microsoft][SQLServer 2000 Driver for JDBC]&#65533;&#65533;&#65533;&#65533;X&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533; : {0}



以上、よろしくお願い申し上げます。




[ メッセージ編集済み 編集者: まいこ 編集日時 2005-07-01 22:31 ]
かつのり
ぬし
会議室デビュー日: 2004/03/18
投稿数: 2015
お住まい・勤務地: 札幌
投稿日時: 2005-07-01 23:22
SQLServerとJDBCドライバは最新ですか?
サービスパックが出ていますが。
kowest
会議室デビュー日: 2005/04/18
投稿数: 5
投稿日時: 2005-07-02 12:01
ireccnt = stmt.executeUpdate(s_sql); ←ここで落ちます。
System.out.println(sql_ex2.getErrorCode()); ←結果1
System.out.println(sql_ex2.getMessage()); ←結果2

これは合っているのかな?
ちょっと違う気がする。

基本的に、正しくStackTraceを出力した方が解決は速まると思うけど・・・
syo
常連さん
会議室デビュー日: 2003/08/17
投稿数: 43
投稿日時: 2005-07-03 15:15
お世話になっております。

引用:

stmt = cone.prepareStatement(s_sql);
ireccnt = stmt.executeUpdate(s_sql); ←ここで落ちます。


の部分を、
stmt = cone.prepareStatement(s_sql);
ireccnt = stmt.executeUpdate();
にしてもだめでしょうか?

今手元にデータベースを扱える環境がない(&もしかしたらドライバによるかもしれない)ので、外しているかもしれませんが・・・。

ふと思ったのですが、テーブル構造がわかりませんが、その構造と実際に登録しようとしているデータの型が違うって言うことはないんですよね??(int型のところに'1111'(Char型)を格納しようとしているとか)
まいこ
会議室デビュー日: 2005/07/01
投稿数: 9
投稿日時: 2005-07-04 22:32
かつのり様・kowest様・syo様

お忙しい中レスありがとうございました。

JDBCドライバは、先日
http://www.microsoft.com/japan/msdn/sqlserver/downloads/jdbc/default.asp
より落としてきまして、最新かと思います。

> ireccnt = stmt.executeUpdate(s_sql); ←ここで落ちます。
> System.out.println(sql_ex2.getErrorCode()); ←結果1
> System.out.println(sql_ex2.getMessage()); ←結果2
(引用のやりかたがよくわからないので上記の書き方でごめんなさい・・・。)
申し訳ございません。ここではなく、もう少し上の
System.out.println(sql_ex.getErrorCode());
System.out.println(sql_ex.getMessage());
ここでした・・・。
次回に質問させていただくときは、気をつけていきます。

> stmt = cone.prepareStatement(s_sql);
> ireccnt = stmt.executeUpdate();
このようにしたら、見事に動きました!
よくよくexecuteUpdate()についてみてみたら、
戻り値がprepareStatement型のものは、引数がいらなかったのですね!


初投稿ということで、勝手がわからず無茶な投稿をしてしまいまして、
ご迷惑をおかけしました。
次回に質問をさせていただくときは、皆様のアドバイスを踏まえて、
投稿させていただきます!

本当にありがとうございました!
1

スキルアップ/キャリアアップ(JOB@IT)