首页 > 代码库 > jdbc的增删改查

jdbc的增删改查

package com.oracle.mysql;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) throws Exception  {
    change();
    }

public static void login() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");
     Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger");
        Statement st=conn.createStatement();
    Scanner s=new Scanner(System.in);
    System.out.println("请输入用户名");
    String name=s.nextLine();
   System.out.println("请输入密码");
   String password=s.nextLine();
   String sql="select * from tb_user where userName=? and password=?";
PreparedStatement ps=conn.prepareStatement(sql);
   ps.setString(1, name);
   ps.setString(2, password);
   ResultSet rs=ps.executeQuery();
   if(rs.next()){
       System.out.println("登陆成功");
   }else{
       System.out.println("用户名或密码错误");
   }
   conn.close();
   st.close();
   ps.close();
   rs.close();
}

public static void add() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");
     Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger");
        Statement st=conn.createStatement();
        Scanner s=new Scanner(System.in);
        System.out.println("请输入书名");
       String bookname=s.nextLine();
        System.out.println("价格");
        int price=s.nextInt();
        System.out.println("出版时间");
        String publish=s.next();
        String sql="insert into book(bookname,price,creatdate)values(?,?,?)";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, bookname);
        ps.setInt(2, price);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date(sdf.parse(publish).getTime());
        ps.setDate(3, date);
        ps.execute();
        
        conn.close();
        st.close();
        ps.close();
    
}
public static void delete() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger");
PreparedStatement ps=conn.prepareStatement("delete from book where bookid=?");
Scanner s=new Scanner(System.in);
System.out.println("请输入用户编号");
int id=s.nextInt();
ps.setInt(1, id);
ps.execute();
conn.close();
ps.close();
}
public static void find() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger");
       Statement st=conn.createStatement();
      ResultSet rs=st.executeQuery("select * from book");
      while(rs.next()){
          System.out.print(rs.getInt(1)+"\t");
          System.out.print(rs.getString(2)+"\t");
          System.out.print(rs.getInt(3)+"\t");
          System.out.println(rs.getString(4)+"\t");
          System.out.println(rs.getDate(5)+"\t");
       }
       rs.close();
       st.close();
       conn.close();
}
public static void change() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");
     Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger");
        Statement st=conn.createStatement();
        Scanner s=new Scanner(System.in);
        System.out.println("请输入id");
       int bookid=s.nextInt();
        System.out.println("价格");
        int price=s.nextInt();
        String sql="update book set price=?  where bookid=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1, price);
        ps.setInt(2, bookid);
        ps.execute();
        
        conn.close();
        st.close();
        ps.close();
    
}
}

 

jdbc的增删改查