Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate » MS SQL  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 关于sql 语言中where like =
爱猫的青蛙





发贴: 18
积分: 0
于 2006-06-05 11:30 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

import java.net.URL;
import java.sql.*;
class Chaxun
{
public static void main(String args[])
{
String l="jdbc:odbc:PUB";
try
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNOtFoundException:");
System.err.println(e.getMessage());
}
Connection con=DriverManager.getConnection(l,"sa","kelly");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(" select * from titles where pub_id='1389' ");
System.out.println("This is content"+rs.getString("title_id"));
con.close();
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("数据库异常被捕获了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
}
}
}
}

我运行这个程序的时候
总是说无效的游标状态
但是表中的确有这个纪录的
关于这句where pub_id='1389'我用=和like都不行
还想问问,如果一个字符串只知道前3位
想查找它该如何书写呢

谢谢


why edited on 2006-06-05 12:38

作者 Re:关于sql 语言中where like = [Re:爱猫的青蛙]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-06-05 12:45 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1. You should
rs.next();
before
rs.getString(...);

2. like '123%'
% is the wildcard (any number of characters) in SQL



作者 Re:关于sql 语言中where like = [Re:爱猫的青蛙]
爱猫的青蛙





发贴: 18
积分: 0
于 2006-06-05 13:51 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢啊
果然是这里的问题

但是请问用rs.next()
有什么作用?

不太懂



作者 Re:关于sql 语言中where like = [Re:爱猫的青蛙]
爱猫的青蛙





发贴: 18
积分: 0
于 2006-06-05 21:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
next我知道作用了

但是我还想请问一下
那如果用like这个匹配有多条记录相符合,那如何读取
呢?

这个程序中返回的只是所找到的第一条记录阿?

谢谢



作者 Re:关于sql 语言中where like = [Re:爱猫的青蛙]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-06-05 22:01 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
actually you should have checked whether rs is empty (i.e. if rs.next() { ... })

while (rs.next()) {
...
... = rs.getXXX(...);
...
}




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923