JDBC-SqlServer增删改查例子

SqlServer增删改查例子


这是之前做过的,当时学校教的微软的SqlServer2008,所以数据库是这个,但是大同小异,
目前我使用MySql,以下代码是连接SqlServer2008的。
前提:
下载好相应版本的驱动jar包 下载链接http://download.csdn.net/detail/acm_th/9190425


代码如下:

package com.example.homework;

import java.sql.*;

//定义接口
interface SqlInter {
    void insertData(String name, String note, float price, int amount); //插入数据方法

    void updateData(String name, int pid);  // 更新数据方法

    void deleteData(int pid);  // 删除数据方法

    void queryData();  // 查询数据方法
}

public class SqlDemo implements SqlInter {

    private PreparedStatement pstat = null;
    private ResultSet rs = null;
    private String forname = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private String getConnection = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=product";
    private String admin = "sa";
    private String pass = "123456";
    private Connection con = null;

    public SqlDemo() {
        try {
            Class.forName(forname);// 加载驱动器
            con = DriverManager.getConnection(getConnection, admin, pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 删除数据
    public void deleteData(int pid) {
        try {
            String seek = "DELETE Product WHERE pid = ?";
            pstat = con.prepareStatement(seek);
            pstat.setInt(1, pid);

            int res = pstat.executeUpdate();
            System.out.println(res > 0 ? "删除成功" : "删除失败");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 更新数据
    public void updateData(String name, int pid) {

        try {
            String seek = "UPDATE Product SET name = ? WHERE pid = ?";
            pstat = con.prepareStatement(seek);
            pstat.setString(1, name);
            pstat.setInt(2, pid);
            int res = pstat.executeUpdate();
            System.out.println(res > 0 ? "更新成功" : "更新失败");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 插入数据
    public void insertData(String name, String note, float price, int amount) {

        try {
            String seek = "INSERT Product VALUES (?, ?, ?, ?)";
            pstat = con.prepareStatement(seek);
            pstat.setString(1, name);
            pstat.setString(2, note);
            pstat.setFloat(3, price);
            pstat.setInt(4, amount);

            int res = pstat.executeUpdate();
            System.out.println(res > 0 ? "插入数据成功" : "插入数据失败");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 查询方法 查询全部数据
    public void queryData() {
        try {
            String seek = "SELECT * FROM Product";
            pstat = con.prepareStatement(seek);
            rs = pstat.executeQuery();// 发送查询
            while (rs.next()) {
                System.out.println("产品编号" + rs.getString(1) + "\n" + "产品名称"
                        + rs.getString(2) + "\n" + "产品简介" + rs.getString(3)
                        + "\n" + "产品单价" + rs.getString(4) + "\n" + "产品数量"
                        + rs.getString(5));
                System.out.println("");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new SqlDemo();
    }
}

你可能感兴趣的:(MySql,JDBC)