package com.lianxi; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; 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(); } }
时间: 2024-11-05 17:26:29