1. 程式人生 > >jdbc的增刪改查

jdbc的增刪改查

create chang query set ext pack sim stat parse

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的增刪改查