JDBC操作数据库

1、准备所需要的jar包 下载地址:https://mvnrepository.com/

2、编写一个实体类

package jdbctest;

import java.util.Date;

public class Mombile {
	String mobiletype ;// 类型
	String mobiledesc;// 描述
	double price;// 价格
	Date ontime;//上架时间

	public String getMobiletype() {
		return mobiletype;
	}

	public void setMobiletype(String mobiletype) {
		this.mobiletype = mobiletype;
	}

	public String getMobiledesc() {
		return mobiledesc;
	}

	public Date getOntime() {
		return ontime;
	}

	public void setOntime(Date ontime) {
		this.ontime = ontime;
	}

	public void setMobiledesc(String mobiledesc) {
		this.mobiledesc = mobiledesc;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

}

3、编写jdbc连接数据库的方法以及增删查改的方法

package jdbctest;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;



public class JdbcCrudTest {
	public static void main(String[] args) {
		Mombile mombile =new Mombile();
		mombile.setMobiletype("Iphone");
		mombile.setMobiledesc("苹果手机");
        mombile.setPrice(2499);
        try {
			mombile.setOntime(new SimpleDateFormat("yyyy-MM-dd").parse("2018-6-1"));//date类型时间
		} catch (ParseException e) {
			e.printStackTrace();
		}
        //增加操作
        if(insert(mombile)>0){
        	System.out.println("添加成功");
        }
        //删除操作
        if(delete("22")>0){
        	System.out.println("删除成功");
        }
        //修改操作 此处id写死了
        if(update(mombile, "21")>0){
        	System.out.println("修改成功");
        }
        //查看
        List<Mombile> mombiles=getAll();
        for (int i = 0; i < mombiles.size(); i++) {
			System.out.println(mombiles.get(i).mobiletype+";"+mombiles.get(i).mobiledesc+";"+mombiles.get(i).ontime+";"+mombiles.get(i).price);
		}
        
	}


	static  Connection getConnection(){
		//final String URL = "jdbc:oracle:thin:localhost:1521:orcl?useUnicode=true&amp;characterEncoding=utf-8";
		final String URL = "jdbc:mysql://localhost:3306/mystudy?characterEncoding=utf-8";
		final String USER = "root";
		final String PASSWORD = "root";
		Connection conn = null;
	
         try {
             //1.加载驱动程序
             Class.forName("com.mysql.jdbc.Driver");
        	 //Class.forName("oracle.jdbc.driver.OracleDriver");
             //2.获得数据库的连接
             conn=DriverManager.getConnection(URL, USER, PASSWORD);
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
	         return conn;
	}
	
	@SuppressWarnings("unused")
	private static int insert(Mombile mombile) {
	    Connection conn = getConnection();
	    int i = 0;
	    String sql = "insert into mobile (mobiletype,mobiledesc,price,ontime) values(?,?,?,?)";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
	        pstmt.setString(1, mombile.getMobiletype());
	        pstmt.setString(2, mombile.getMobiledesc());
	        pstmt.setDouble(3, mombile.getPrice());;
	        pstmt.setDate(4, new java.sql.Date(mombile.getOntime().getTime()));//重点 转换dete类型的数据
	        i = pstmt.executeUpdate();
	        pstmt.close();
	        conn.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return i;
	}
	
	@SuppressWarnings("unused")
	private static int delete(String id) {
	    Connection conn = getConnection();
	    int i = 0;
	    String sql = "delete from mobile where id='" + id + "'";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
	        i = pstmt.executeUpdate();
	        pstmt.close();
	        conn.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return i;
	}
	
	@SuppressWarnings("null")
	private static List<Mombile> getAll() {
		List<Mombile> list=new ArrayList<Mombile>(); 
	    Connection conn = getConnection();
	    String sql = "select mobiledesc,mobiletype,ontime,price from mobile";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement)conn.prepareStatement(sql);
	        ResultSet rs = pstmt.executeQuery();
	        Mombile mombile = new Mombile();
	        while (rs.next()) {
	        	mombile.setMobiledesc(rs.getString("mobiledesc"));
	        	mombile.setMobiletype(rs.getString("mobiletype"));
	        	mombile.setOntime(rs.getDate("ontime"));
	        	mombile.setPrice(rs.getDouble("price"));
	        	list.add(mombile);
	        }
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return list;
	}
	
	@SuppressWarnings("unused")
	private static int update(Mombile mombile,String string) {
	    Connection conn = getConnection();
	    int i = 0;
	    String sql = "UPDATE mobile SET mobiledesc='"+mombile.getMobiledesc()+"',mobiletype='"+mombile.getMobiletype()+"',"
	    		+ "ontime='"+new java.sql.Date(mombile.getOntime().getTime())+"',price="+mombile.getPrice()+" WHERE id="+string+"";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
	        i = pstmt.executeUpdate();
	        pstmt.close();
	        conn.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return i;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值