必备sql语句
1.读出表中的字段名
ResultSet rs = test.selectSql("SELECT * from datainfo");
java.sql.ResultSetMetaData md=rs.getMetaData(); //读出数据库的字段名
int nColumn=md.getColumnCount(); //字段总数
for(int i=0;i<nColumn;i++)
{
System.out.println(md.getColumnLabel(i+1)); //md.getColumnLabel(n)n是从1开始的,打印每个字段
}
2.查询某个时间段内的数据
String sql="select * from datainfo where time between '"+start+"' and '"+end+"'"; 取时间段内的数据
select * from datainfo where time between "2009-11-03 11:16:59"and "2009-11-03 11:18:59"
3.按某个字段降序排列并选取第一个
sql="select * from datainfo,nodeinfo where nodeinfo.nodeid="+Integer.parseInt(param)+" and nodeinfo.nodeip=datainfo.ip
order by time desc limit 0,1"; 按时间降序排列 只取第一个
(limit 0,1 是只取记录中的第一条.所以这条语句只能得到一条记录
如想取前10条则 limit 0,10或limit 10
如想取第10至20条则 limit 10,20 )
4.出现Operation not allowed after ResultSet closed的错误
一个stmt多个rs进行操作.那么从stmt得到的rs1,必须马上操作此rs1后,才能去得到另外的rs2,再对rs2操作.不能互相交替使用,会引起rs已经关闭错误. 错误的代码如下:
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from t1");
rst=stmt.executeQuery("select * from t2"); rs.last();
//由于执行了rst=stmt.executeQuery(sql_a);rs就会被关闭掉!所以程序执行到此会提示ResultSet已经关闭.
错误信息为:java.sql.SQLException: Operation not allowed after ResultSet closed rst.last();
正确的代码:
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from t1");
&n
相关文档:
在SQL Server里查看当前连接的在线用户数
use master
select loginame,count(0) from sysprocesses
group by loginame
order by count(0) desc
select nt_username,count(0) from sysprocesses
group by nt_username
order by count(0) desc
如果某个SQL Server用户名test连接比较多,查看它来自的主机名:
......
1、select 1 from mytable;与select anycol(目的表集合中的任意一行) from mytable;与select * from mytable 作用上来说是没有差别的,都是查看是否有记录,一般是作条件用的。select 1 from 中的1是一常量,查到的所有行的值都是它,但从效率上来说,1>anycol>*,因为不用查字典表。
2、查看记录条数可以用select ......
SQL中CONVERT函数最常用的是使用convert转化长日期为短日期
如果只要取yyyy-mm-dd格式时间, 就可以用 convert(nvarchar(10),field,120)
120 是格式代码, nvarchar(10) 是指取出前10位字符.
SELECT CONVERT(nvarchar(10), getdate(), 120)
SELECT CONVERT(varchar(10), getdate(), 120)
SELECT CONVERT(char(10), ge ......
1. 创建视图:
CREATE OR REPLACE VIEW SM_V_UNIT_AUTH AS
SELECT T2.UNIT_ID,
T2.SUPER_UNIT_ID,
T1.AUTH_ID,
T1.AUTH_NAME,
T1.A ......
Introducing Oracle Database 11g
List the features of Oracle Database 11g
Discuss the basic design, theoretical and physical aspects of a relational database
Categorize the different types of SQL statements
Describe the data set used by the course
Log onto the database using the SQL Develope ......