窗口实现对mysql增删改查_巧用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.printStackT

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值