美文网首页
数据库增加数据代码

数据库增加数据代码

作者: 龙龙龙儿 | 来源:发表于2021-12-30 14:15 被阅读0次

    package com.sjk.test;

    import java.sql.Connection;

    import java.sql.SQLException;

    import java.sql.Statement;

    import java.util.Scanner;

    import com.shk.util.GetConnection;

    public class Test1 {

    public static void main(String[] args) {

    Connection conn=null;

    Statement stmt=null;

    conn=GetConnection.getConn();

    Scanner s=new Scanner(System.in);

    System.out.println("请输入id:");

    int id=s.nextInt();

    System.out.println("请输入姓名:");

    String name=s.next();

    System.out.println("请输入年龄:");

    int age=s.nextInt();

    System.out.println("请输入性别:");

    String sex=s.next();

    //编写SQL语句

    String sql="insert into stu(id,name,age,sex)"

    +"values ("+id+",'"+name+"',"+age+",'"+sex+"');";

    try {

    //Statement对象用来执行sql

    stmt=conn.createStatement();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    try {

    //执行sql

    stmt.execute(sql);

    } catch (SQLException e) {

    e.printStackTrace();

    }finally {

    try {

    conn.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    try {

    stmt.close();

    } catch (SQLException e) {

    e.printStackTrace();

    }

    }

    }

    }

    相关文章

      网友评论

          本文标题:数据库增加数据代码

          本文链接:https://www.haomeiwen.com/subject/orviqrtx.html