数据库增加数据代码

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();

}

}

}

}

你可能感兴趣的:(数据库增加数据代码)