向数据库里插入


package com.lianxi;


import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
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();
System.out.println("出版时间");
String publish=s.next();
//两行代码赋值
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();

}


}

 
posted @ 2017-05-20 16:31  苏轼的红烧肉  阅读(143)  评论(0编辑  收藏  举报