zoukankan      html  css  js  c++  java
  • MySQL插入数据

    package MySQLExercise;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    import java.text.SimpleDateFormat;

    public class ExerciseFirst {
    public static final String DBDRIVER="com.mysql.jdbc.Driver";
    public static final String DBURL="jdbc:mysql://127.0.0.1:3306/javademo";
    public static final String DBUSER="root";
    public static final String DBPASS="123";
    public static void main(String args[])throws Exception{
    Connection conn=null;
    Statement stmt=null;
    PreparedStatement pstmt=null;

    BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
    String name=null;
    String password=null;
    int age=0;
    String sex=null;
    String birthday=null;

    System.out.print("Please input name : ");
    name=buff.readLine();
    System.out.print("Please input password : ");
    password=buff.readLine();
    System.out.print("Please input age : ");
    String agestr=buff.readLine();
    age=Integer.parseInt(agestr);
    System.out.print("Please input sex: ");
    sex=buff.readLine();
    System.out.print("Please input birthday : ");
    birthday=buff.readLine();
    java.util.Date temp=null;
    temp = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
    java.sql.Date bir=new java.sql.Date(temp.getTime());

    String sql="insert INTO user(name,password,age,sex,birthday) values(?,?,?,?,?)";
    Class.forName(DBDRIVER);
    conn= DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    pstmt=conn.prepareStatement(sql);

    pstmt=conn.prepareStatement(sql);
    pstmt.setString(1,name);
    pstmt.setString(2,password);
    pstmt.setInt(3,age);
    pstmt.setString(4,sex);
    pstmt.setDate(5,bir);
    pstmt.executeUpdate();
    System.out.println("Insert successfully . ");
    pstmt.close();
    conn.close();

    }
    }
  • 相关阅读:
    Cousera课程Learning How to Learn学习报告
    C语言中当无符号数遇到符号数
    STC15 串口(工作方式1)使用小结
    取C语言头文件的文件名
    linux 的 shell 逻辑
    Win7 局域网内简单共享的设置
    写了一个批处理,可以实现文件备份,自动对比删除冗余文件。
    C语言 函数指针的应用
    自动控制原理 典型环节频率特性对比
    51单片机汇编的溢出标志位OV和进位标志位CY
  • 原文地址:https://www.cnblogs.com/ssMellon/p/6508116.html
Copyright © 2011-2022 走看看