巧用JDBC实现对MySQL的“增删改查”

作者:10933 时间:2008-12-31 15:12:00 

用JDBC实现对MySQL的“增删改查”:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import com.bean.NoticeBean;

public class JDBCTest {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection conn=null;

Statement stmt=null;

ResultSet rs=null;

try {

String driverName="com.mysql.jdbc.Driver";

Class.forName(driverName);

String url="jdbc:mysql://localhost:3306/java?

useUnicode=true&characterEncoding=gb2312";

conn=DriverManager.getConnection(url,"root","root");

System.out.println("连接MySql成功!!!");

stmt=null;

rs=null;

String strSql=null;

NoticeBean bean=null;

String title=null;

String content=null;

try {

title="标题";

content="内容";

strSql="INSERT INTO notice(title,content) VALUES('"+title+"','"+content+"')";

stmt=conn.createStatement();

stmt.executeUpdate(strSql);

System.out.println("插入语句执行成功:"+strSql);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("插入失败");

}

strSql="select * from notice";

stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);

rs=stmt.executeQuery(strSql);

if(rs.next()){

int id=rs.getInt("id");

title =rs.getString("title");

content=rs.getString("content");

if(rs.next()){

bean=new NoticeBean(id,title,content);

}

System.out.println("notice第一行数据是"+bean.getId()+" "+bean.getTitle()

+" "+bean.getContent());

}

try {

strSql="delete from notice";

stmt=conn.createStatement();

stmt.executeUpdate(strSql);

System.out.println("删除完成");

} catch (RuntimeException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("删除失败");

}

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

if(rs!=null){

rs.close();

rs=null;

}

if(stmt!=null){

stmt.close();

stmt=null;

}

if(conn!=null){

conn.close();

conn=null;

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

标签:
0
投稿

猜你喜欢

  • 超越质检员——看图购beta版的思考

    2009-04-15 12:11:00
  • ASP 错误代码

    2009-05-11 12:38:00
  • 《JavaScript语言精粹》

    2009-04-03 11:27:00
  • 如何实现论坛的树状记录表展开技术?

    2010-05-19 21:37:00
  • Ajax:拥抱JSON,让XML走开

    2007-10-12 20:01:00
  • MySQL数据库备份的基础知识大全

    2009-12-20 18:14:00
  • W3C优质网页小贴士(二)

    2008-04-07 12:14:00
  • JavaScript解决Joseph问题

    2008-06-21 17:11:00
  • 认识Javascript数组

    2009-08-27 15:26:00
  • 分类与类型的问题

    2008-09-12 13:10:00
  • 仿google的asp分页代码

    2009-03-08 18:27:00
  • 如何在Unix系统环境下安装MySQL数据库

    2009-01-04 13:09:00
  • 用文本+ASP打造新闻发布系统

    2009-02-02 09:31:00
  • 将字符实体引用转换成 Unicode 字符

    2011-07-01 12:31:51
  • 一直闪烁变色的超级链接代码

    2008-02-27 13:08:00
  • 为SQL Server数据库传数组参数的变通办法

    2009-10-23 09:26:00
  • asp如何随机显示网站链接?

    2010-06-07 20:40:00
  • 利用global.asa计划执行程序

    2008-03-05 12:49:00
  • SQL Server数据转换服务基本概念介绍

    2009-01-20 15:52:00
  • 关于设计的一些个人看法

    2008-06-12 12:44:00
  • asp之家 网络编程 m.aspxhome.com