当前位置:首页 > 网络编程 > 数据库 > Mysql > 巧用JDBC实现对MySQL的“增删改查”

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

点击次数:38 次 发布日期:2009-01-03 22:16:50 作者:源代码网
源代码网推荐

源代码网整理以下用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)
会员中心
网络编程
本站推荐
网络编程之精华