PreparedStatement(预编译)
package com.lianxi; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.util.Scanner; public class AddBook { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger"); //创建预编译的PS PreparedStatement ps=conn.prepareStatement("insert into book(bookname,price,creatdate)values(?,?,?)"); String bookname; int price; Scanner s=new Scanner(System.in); System.out.println("请输入书名"); bookname=s.next(); System.out.println("价格"); price=s.nextInt(); //两行代码赋值 ps.setString(1, bookname); ps.setInt(2, price); ps.setDate(3, new Date(116,6,7)); //new Date方法不好 ps.execute(); conn.close(); } }