向数据库里插入
时间:2021-07-01 10:21:17
帮助过:25人阅读
public class Test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Class.forName(
"com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/boooks",
"root",
"tiger");
Statement st=
conn.createStatement();
String bookname;
int price;
String author;
String date;
Scanner s=
new Scanner(System.
in);
System.out.println(
"请输入书名");
bookname=
s.next();
System.out.println(
"价格");
price=
s.nextInt();
System.out.println(
"请输入作者");
author=
s.next();
System.out.println(
"请输入日期");
date=
s.next();
//拼一个sql语句
String sql=
"insert into book values(null,‘"+bookname+
"‘,"+price+
",‘"+author+
"‘,‘"+date+
"‘)";
System.out.println(sql);
st.execute(sql);
System.out.println(
"插入成功");
st.close();
conn.close();
}
}
向数据库里插入
标签:new stat package import roo eth auto sql语句 val