java如何插入数据进sql

Python019

java如何插入数据进sql,第1张

(最基本的连接方法)

1。获取连接

获取连接需要两步,

一是使用DriverManager来注册驱动(Class.forName(“com.mysql.jdbc.Driver”)),二是使用DriverManager来获取Connection对像DriverManager.getConnection(url,username,password)

2.获取Statement(Statement stmt =con.createStatement())

Statement就是执行sql语句的;

3.执行sql语句

String sql = “insertinto user value(’zhangSan’, ’123’)”

int m =stmt.executeUpdate(sql)

//总代码如下

publicstatic Connection getConnection() throws Exception {

Class.forName("com.mysql.jdbc.Driver")

String url = "jdbc:mysql://localhost:3306/mydb1"

return DriverManager.getConnection(url, "root", "123")

}

@Test

publicvoid insert() throws Exception {

Connection con = getConnection()

Statement stmt = con.createStatement()

String sql = "insert into user values('zhangSan', '123')"

stmt.executeUpdate(sql)

System.out.println("插入成功!")

}

public int save(Notices notice) throws SQLException{

String sql = "insert into noticeinfo(notice_title,notice_type,notice_content,notice_add_time,user_id,user_table_id,class_table_id,notice_state,is_important)"+"values(?,?,?,?,?,?,?,?,?)"

PreparedStatement pstmt = super.getConnection().prepareStatement(sql)

pstmt.setString(1, notice.getNotice_title())

pstmt.setInt(2, notice.getNotice_type())

pstmt.setString(3, notice.getNotice_content())

pstmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()))

pstmt.setString(5, notice.getUser_id())

pstmt.setInt(6, notice.getUser_table_id())

pstmt.setInt(7, notice.getClass_table_id())

pstmt.setInt(8, notice.getNotice_state())

pstmt.setInt(9, notice.getIs_important())

int i = pstmt.executeUpdate()

return i

}

/**

* Copyright 2014 (C) PANLAB ,All Rights Reserved.

*/

package com.lrlz.common.tool

import java.text.ParsePosition

import java.text.SimpleDateFormat

import java.util.Calendar

import java.util.Date

import java.util.GregorianCalendar

/**

* <p>Title: 基础类</p>

* <p>Description: 日期转换</p>

* <p>Company: </p>

* @version 1.0

*/

public class DateUtils {

/**

* 日期转化为字符串

* @param date 时间

* @return yyyy-MM-dd HH:mm:ss 格式化的时间字符串

*/

public static String dateToString(Date date) {

if(date==null) return ""

return FormatDate(date, "yyyy-MM-dd HH:mm:ss")

}

/**

* 日期转化为字符串

* @param date 时间

* @return yyyy-MM-dd 格式化的时间字符串

*/

public static String dateToStringShort(Date date) {

if(date==null) return ""

return FormatDate(date, "yyyy-MM-dd")

}

/**

* 计算两个日期差(毫秒)

* @param date1 时间1

* @param date2 时间2

* @return 相差毫秒数

*/

public static long diffTwoDate(Date date1, Date date2) {

long l1 = date1.getTime()

long l2 = date2.getTime()

return (l1 - l2)

}

/**

* 计算两个日期差(毫秒)

* @param date1 时间1

* @param date2 时间2

* @return 相差毫秒数

*/

public static int diffMinterDate(Date date1, Date date2) {

if(date1==null||date2==null){

return 0

}

long l1 = date1.getTime()

long l2 = date2.getTime()

int deff=Integer.parseInt(""+(l1-l2)/1000/60)

return deff

}

/**

* 计算两个日期差(天)

* @param date1 时间1

* @param date2 时间2

* @return 相差天数

*/

public static int diffTwoDateDay(Date date1, Date date2) {

long l1 = date1.getTime()

long l2 = date2.getTime()

int diff = Integer.parseInt(""+(l1 - l2)/3600/24/1000)

return diff

}

/**

* 对日期进行格式化

* @param date 日期

* @param sf 日期格式

* @return 字符串

*/

public static String FormatDate(Date date, String sf) {

if(date==null) return ""

SimpleDateFormat dateformat = new SimpleDateFormat(sf)

return dateformat.format(date)

}

/**

* 取得当前系统日期

* @return yyyy-MM-dd

*/

public static String getCurrDate() {

Date date_time = new Date()

return FormatDate(date_time, "yyyy-MM-dd")

}

//取系统时间时一定要用这个方法,否则日期可能不动

public static Date getCurrDateTime(){

return new Date(System.currentTimeMillis())

}

/**

* 返回格式化时间

* @param fmt

* @return

*/

public static String getCurrDateTime(String fmt){

return FormatDate(new Date(System.currentTimeMillis()),fmt)

}

/**

* 取得当前系统时间

* @return yyyy-MM-dd HH:mm:ss

*/

public static String getCurrTime() {

Date date_time = new Date()

return FormatDate(date_time, "yyyy-MM-dd HH:mm:ss")

}

/**

* 取得日期的天份

* @param date 日期

* @return dd 天字符串

*/

public static String getDay(Date date) {

return FormatDate(date, "dd")

}

/**

* 取得日期的小时

* @param date 日期

* @return hh 小时字符串

*/

public static String getHour(Date date) {

return FormatDate(date, "HH")

}

/**

* 取得日期的分钟

* @param date 时间

* @return mm 分钟字符串

*/

public static String getMinute(Date date) {

return FormatDate(date, "mm")

}

/**

* 取得日期的月份

* @param date 日期

* @return mm 月份字符串

*/

public static String getMonth(Date date) {

return FormatDate(date, "MM")

}

public static int getMonth(Date start, Date end) {

if (start.after(end)) {

Date t = start

start = end

end = t

}

Calendar startCalendar = Calendar.getInstance()

startCalendar.setTime(start)

Calendar endCalendar = Calendar.getInstance()

endCalendar.setTime(end)

Calendar temp = Calendar.getInstance()

temp.setTime(end)

temp.add(Calendar.DATE, 1)

int year = endCalendar.get(Calendar.YEAR)

- startCalendar.get(Calendar.YEAR)

int month = endCalendar.get(Calendar.MONTH)

- startCalendar.get(Calendar.MONTH)

if ((startCalendar.get(Calendar.DATE) == 1)

&&(temp.get(Calendar.DATE) == 1)) {

return year * 12 + month + 1

} else if ((startCalendar.get(Calendar.DATE) != 1)

&&(temp.get(Calendar.DATE) == 1)) {

return year * 12 + month

} else if ((startCalendar.get(Calendar.DATE) == 1)

&&(temp.get(Calendar.DATE) != 1)) {

return year * 12 + month

} else {

return (year * 12 + month - 1) <0 ? 0 : (year * 12 + month)

}

}

/**

* 取得时间的秒

* @param date 时间

* @return ss 秒字符串

*/

public static String getSecond(Date date) {

return FormatDate(date, "ss")

}

/**

*根据年、月取得月末的日期

* @param year 年

* @parm month 月

* @return time 返回日期格式"yyyy-mm-dd"

*/

public static String getTime(String year,String month){

String time=""

int len=31

int iYear=Integer.parseInt(year)

int iMonth=Integer.parseInt(month)

if(iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11)

len=30

if(iMonth==2){

len=28

if((iYear%4==0 &&iYear%100==0 &&iYear%400==0) || (iYear%4==0 &&iYear%100!=0)){

len=29

}

}

time=year+"-"+month+"-"+String.valueOf(len)

return time

}

/**

* 取得日期的年份

* @param date 日期

* @return yyyy 年份字符串

*/

public static String getYear(Date date) {

return FormatDate(date, "yyyy")

}

/**

* 字符串转换为日期

* @param dateString yyyy-MM-dd HH:mm:ss

* @return 日期

*/

public static Date stringToDate(String dateString) {

if(dateString==null || dateString.trim().length()==0) return null

String datestr = dateString.trim()

String sf = "yyyy-MM-dd HH:mm:ss"

Date dt = stringToDate(datestr, sf)

if(dt==null) dt = stringToDate(datestr, "yyyy-MM-dd")

if(dt==null) dt = stringToDate(datestr, "MM-dd HH:mm:ss")

if(dt==null) dt = stringToDate(datestr, "dd HH:mm:ss")

if(dt==null) dt = stringToDate(datestr, "yyyyMMdd")

return dt

}

/** 字符串转换为日期

* @param dateString 日期格式字符串

* @param sf 日期格式化定义

* @return 转换后的日期

*/

public static Date stringToDate(String dateString, String sf) {

ParsePosition pos = new ParsePosition(0)

SimpleDateFormat sdf = new SimpleDateFormat(sf)

Date dt = sdf.parse(dateString, pos)

return dt

}

/**

* 字符串转换为日期

* @param dateString yyyy-MM-dd

* @return 日期

*/

public static Date stringToDateShort(String dateString) {

String sf = "yyyy-MM-dd"

Date dt = stringToDate(dateString, sf)

return dt

}

public DateUtils() {

}

/**

* 获取格式化容器

* @param fmt

* @return

*/

public static SimpleDateFormat getSimFormat(String fmt){

if(StringUtils.isBlank(fmt))fmt=DATE_YMDHMS

SimpleDateFormat dateFormat = new SimpleDateFormat(fmt)

dateFormat.setLenient(false)

return dateFormat

}

}