qq_26496107 2015-03-11 13:46 采纳率: 0%
浏览 1639

对于java操作mysql数据库删除一行的问题;本人菜鸟跪求

package org.demo.test;

import java.sql.*;

public class SimpleConnectMysqlDemo {

public static void main(String[] args) {

    // 驱动程序名
    String driver = "com.mysql.jdbc.Driver";

    // URL指向要访问的数据库名scutcs
    String url = "jdbc:mysql://127.0.0.1:3306/school?characterEncoding=utf-8";

    // MySQL配置时的用户名
    String user = "root";

    // MySQL配置时的密码
    String password = "root";

    // 获取当前环境编码
    // System.out.println(Charset.defaultCharset());

    try {
        Class.forName(driver);

        // 获取数据库连接
        Connection conn = DriverManager.getConnection(url, user, password);
        if (!conn.isClosed())
            System.out.println("Succeeded connecting to the Database!");

        // statement用来执行SQL语句
        Statement statement = conn.createStatement();


        String sql1 = "select id,username,birthday,image from user";
        String hehe ="insert into user( id,username,birthday,image) "
                + "values(NULL,'张三丰','2011/1/1','190.12')";
        //String sql2="delete * from table where name='name'";
        //String haha="delete * from school where name='张三丰'";
        int tag = statement.executeUpdate(hehe);

        // 结果集
        ResultSet rs = statement.executeQuery(sql1);

        while (rs.next()) {

            System.out.println(rs.getString("id") + 
                    "\t" + rs.getString("username")+ 
                    "\t" + rs.getString("birthday"));
        }

        // 关闭数据库,释放资源
        close(rs, statement, conn);

    } catch (ClassNotFoundException e) {        
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

/**
 * 释放资源,释放资源的顺序应该按照释放结果集,释放Statement,释放数据库连接的顺序并且,为了不占用太多的资源,
 * 数据库连接建立到释放的时间应该尽量短。
 * 
 * @param rs
 * @param st
 * @param con
 */
public static void close(ResultSet rs, Statement st, Connection con) {
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (st != null) {
        try {
            st.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (con != null) {
        try {
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

  • 写回答

1条回答 默认 最新

  • oyljerry 2015-03-11 15:29
    关注

    delete from table where name = xxx

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算