import java.sql.Connection;
import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.Date; public class Test { private static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=RMS_db"; private static String userName = "sa"; private static String userPwd = "root"; public static void main(String[] srg) { try { Class.forName(driverName); Test.addTempBill(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 添加一个空桌订单 */ public static void addTempBill() { Connection conn = null; Statement state = null; ResultSet result = null; try { conn = DriverManager.getConnection(dbURL, userName, userPwd); state = conn.createStatement(); // 获取最后一个未付款订单 result = state .executeQuery("select top 1 BillID from bi_TempBill order by CreateTime desc"); String code = "0001"; while (result.next()) { String BillID = result.getString("BillID"); int id = Integer.parseInt(BillID.split("-")[2]); id++; code = String.valueOf(id); if (code.length() < 4) { String temp = ""; for (int i = code.length(); i < 4; i++) { temp += "0"; } code = temp + code; } SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd"); code = "XS-" + sdf.format(new Date()) + "-" + code; } Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = sdf.format(date); int re = state .executeUpdate("insert into bi_TempBill (BillID,CreateTime) values('" + code + "','" + dateStr + "') "); System.out.println(re); } catch (SQLException e) { addTempBill(); } finally { try { result.close(); state.close(); conn.close(); } catch (Exception e) { } } } } (责任编辑:IT) |