java对SQL查询多条怎么查?

2025-01-21 02:47:46
推荐回答(3个)
回答1:

最简单的方法是编写JDBC,就能传入sql语句。

要查2012-3-6到2012-3-7的怎么做,可以传入:
sql语句:
select *from from UserTable u where u.date between '2012-3-6' and '2012-3-7';
注:如果date不是string类型的话,需要通过相应的函数进行转换,不同的数据库转换函数有点不一样。还有这边也可以使用 u.date>'2012-3-6' and u.date<'2012-3-7'的写法。

回答2:

java通过JDBC连接数据库,通过向数据库发送语句获得结果
比如你要查的日期,语句是
select t.* from timetable t where t.date between '2012-3-6' and '2012-3-7'
现在就用JDBC连接到数据库
import java.sql.*;

public class TimeSql {
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@192.168.0.1:1521:TIME";
public static final String DBUSER = "time";
public static final String DBPASS = "time";
public static void main(String args[]) throws Exception {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from timetable t where t.date between '2012-3-6' and '2012-3-7'"; //发送SQL语句
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
System.out.println(rs.getString(0));
}
rs.close();
pstmt.close();
conn.close();
}
}

回答3:

select count(*) from test; //test是一个数据库文件