首页 > 数据库技术 > 详细

向数据库里插入

时间:2017-05-20 17:08:27      阅读:275      评论:0      收藏:0      [点我收藏+]
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();
    }
    }

 

向数据库里插入

原文:http://www.cnblogs.com/zyy1130/p/6882449.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!