码迷,mamicode.com
首页 > 数据库 > 详细

mysql增删查操作

时间:2016-08-01 21:04:06      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
import com.mysql.jdbc.PreparedStatement;

public class SqlOperate {
    // 连接数据库,并创建表
    public static Connection sqlConnect() throws Exception {
        String url = "jdbc:mysql://192.168.113.17:3306/test";
        String user = "base_hera";
        String password = "Eg6hmE6PDc8cGhZ";
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url, user, password);
        ResultSet result = conn.getMetaData().getTables(null, null, "student", null);
        if (result.next()) {
            System.out.println("该表已经存在");
        } else {
            Statement statement = conn.createStatement();
            String sql = "create table student(name char(20),age int(20),sex char(20))";
            statement.executeUpdate(sql);
            System.out.println("创建表成功");
        }
        return conn;
    }

    // 新增
    public static void add(Connection conn) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = scanner.nextLine();
        System.out.println("请输入性别:");
        String sex = scanner.nextLine();
        System.out.println("请输入年龄:");
        String age = scanner.nextLine();
        String sql = "insert into student (name,sex,age) values(?,?,?)";
        PreparedStatement preparedStatement = null;
        preparedStatement = (PreparedStatement) conn.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, sex);
        preparedStatement.setString(3, age);
        int i = preparedStatement.executeUpdate();
        if (i == 1) {
            System.out.println("新增成功!");
        } else {
            System.out.println("新增失败!");
        }
        scanner.close();
    }

    // 查询
    public static void query(Connection conn) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要查询的用户名:");
        String name = scanner.nextLine();
        String sql = "select * from student where name = ? ";
        PreparedStatement preparedStatement = null;
        preparedStatement = (PreparedStatement) conn.prepareStatement(sql);
        preparedStatement.setString(1, name);
        ResultSet result = preparedStatement.executeQuery();
        // 指针移动至结果集的第一行
        if (result.next()) {
            System.out.println(
                    "姓名:" + result.getString(1) + "  年龄:" + result.getString(2) + "  性别:" + result.getString(3));
        } else {
            System.out.println("对不起,无此用户,不可进行查询");
        }
        scanner.close();
    }

    // 删除
    public static void delete(Connection conn) throws Exception {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要删除的用户名:");
        String name = scanner.nextLine();
        String sql = "delete from student where name= ? ";
        PreparedStatement preparedStatement = null;
        preparedStatement = (PreparedStatement) conn.prepareStatement(sql);
        preparedStatement.setString(1, name);
        int i = preparedStatement.executeUpdate();
        if (i == 0) {
            System.out.println("对不起,无此用户,不可进行删除");
        } else {
            System.out.println("删除成功!");
        }
        scanner.close();
    }

    public static void main(String args[]) throws Exception {
        Connection conn = sqlConnect();
        System.out.println("请选择:" + "\n" + "1.新增用户" + "\n" + "2.查询用户" + "\n" + "3.删除用户");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        if ("1".equals(s)) {
            add(conn);
        } else if ("2".equals(s)) {
            query(conn);
        } else if ("3".equals(s)) {
            delete(conn);
        }
        scanner.close();
    }
}

 

mysql增删查操作

标签:

原文地址:http://www.cnblogs.com/worms/p/5726964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!