`
anysky131
  • 浏览: 172306 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

jsp数据库操作的具体代码

    博客分类:
  • Java
阅读更多
数据库连接语言老是出问题,好好做了一下,然后把东西记录下来.以便给自己一个很深的理解;
1、数据库操作语言;
  1. 数据查询:精选输出的列可以用列名、列别名或列位置在ORDER BYGROUP BY子句引用,列位置从1开始。
    mysql> select college, region, seed from tournament
    ORDER BY region, seed;
    mysql> select college, region AS r, seed AS s from tournament
    ORDER BY r, s;
    mysql> select college, region, seed from tournament
    ORDER BY 2, 3;
因此,对于在table note中查出所有的数据,显示在页面上。
sql=“select * from note”;
对于在table note中按ID查出本条记录,显示在页面上;
sql="select title,author,content from note where id="+id;
2、数据更新:
UPDATE [LOW_PRIORITY] tbl_name SET col_name1=expr1,col_name2=expr2,... [WHERE where_definition] [LIMIT #]

因此,对于在table note中按ID查出本条记录,再进行更新,然后把更新过的数据显示在页面上; sql="update note set title='"+title+"',author='"+author+"',content='"+content+"' where id="+id;

(特别注意的是:在sql语句中,加入变量名时,格式是: 先是一对单引号,里面一对双引号,'"+变量名+"',各个项值之间用逗号。)
3、数据删除:
DELETE [LOW_PRIORITY] FROM tbl_name [WHERE where_definition] [LIMIT rows]
因此,对于在table note中把ID的一条记录删除;
sql="delete from note where id="+id;
4、数据插入:
INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO] tbl_name [(col_name,...)] VALUES (expression,...),(...),...
因此,对于在table note中插入一条记录;
sql="insert into note (title,author,content) values('"+title+"','"+author+"','"+content+"')";
5、数据的精确查询和模糊查询:
if("jingque".equals(chaxunleixing))
{
//精确查询,按留言标题查询;
sql="select * from note where title like '"+keyword+"'";
}else
if("mohu".equals(chaxunleixing))
{
//模糊查询,按关键字查询;(在标题、作者、内容中找有此关键字的相关留言!)
sql="select * from note where title like '%"+keyword+"%'";
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics