package com.company;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by ttc on 18-1-22.
*/
public class JDBCDemoFinal {
//增刪改
public static Sort loc(String username,int password) throws SQLException {
Connection connection = JDBCUtils.getConnection();
Sort Sort = null;
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Sort WHERE username = ? AND PASSWORD = ?");
preparedStatement.setString(1, username);
preparedStatement.setInt(2, password);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Sort = new Sort();
Sort.setSid(resultSet.getInt("sid"));
Sort.setSname(resultSet.getString("sname"));
Sort.setBirthday(resultSet.getDate("birthday"));
Sort.setMail(resultSet.getString("mail"));
Sort.setUsername(resultSet.getString("username"));
Sort.setPassword(resultSet.getInt("password"));
Sort.setRole(resultSet.getInt("role"));
}
return Sort;
}
//修改
public static void updateSort(Sort sort) throws SQLException {
Connection connection = JDBCUtils.getConnection();
PreparedStatement preparedStatement =
connection.prepareStatement("update sort set sname = ?, mail = ?, username = ?, birthday = ?,password=?,role = ? where sid = ?");
preparedStatement.setString(1,sort.getSname());
preparedStatement.setString(2,sort.getMail());
preparedStatement.setString(3,sort.getUsername());
preparedStatement.setInt(5,sort.getPassword());
preparedStatement.setInt(6,sort.getRole());
//java.util.date--->java.sql.Timestamp
Date date = sort.getBirthday();
//得到date對應(yīng)的時間戳
long timestamp = date.getTime();
//通過時間戳創(chuàng)建java.sql.Timestamp
java.sql.Timestamp sqlDate = new java.sql.Timestamp(timestamp);
preparedStatement.setTimestamp(4,sqlDate);
preparedStatement.setInt(7,sort.getSid());
preparedStatement.executeUpdate();
}
//刪除
public static int deleteSortByID(int id) throws SQLException {
Connection connection = JDBCUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("delete from sort where sid = ?");
preparedStatement.setInt(1,id);
int rows = preparedStatement.executeUpdate();
// JDBCUtils.close(preparedStatement,connection);
return rows;
}
//新增
public static void AddSort(Sort sort) throws SQLException {
Connection connection = JDBCUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("insert into sort(sname,mail,username,birthday,password,role) values(?,?,?,?,?,?)");
preparedStatement.setString(1,sort.getSname());
preparedStatement.setString(2,sort.getMail());
preparedStatement.setString(3,sort.getUsername());
preparedStatement.setInt(5,sort.getPassword());
preparedStatement.setInt(6,sort.getRole());
//java.util.date--->java.sql.Timestamp
Date date = sort.getBirthday();
//得到date對應(yīng)的時間戳
long timestamp = date.getTime();
//通過時間戳創(chuàng)建java.sql.Timestamp
java.sql.Timestamp sqlDate = new java.sql.Timestamp(timestamp);
preparedStatement.setTimestamp(4,sqlDate);
preparedStatement.executeUpdate();
// JDBCUtils.close(preparedStatement,connection);
}
//查全部,查一個
public static Sort getSortByID(int id) throws SQLException {
Connection connection = JDBCUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select * from sort where sid = ?");
preparedStatement.setInt(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
Sort sort = null;
if (resultSet.next() == true) {
sort = new Sort();
int sid = resultSet.getInt("sid");
String sname = resultSet.getString("sname");
String username = resultSet.getString("username");
Integer password = resultSet.getInt("password");
Date birthday = resultSet.getTimestamp("birthday");
String mail = resultSet.getString("mail");
Integer role = resultSet.getInt("role");
sort.setSid(sid);
sort.setUsername(username);
sort.setSname(sname);
sort.setPassword(password);
sort.setBirthday(birthday);
sort.setMail(mail);
sort.setRole(role);
}
return sort;
}
public static List<Sort> getSortBysname(String sname) throws SQLException {
Connection connection = JDBCUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select * from sort where sname LIKE ?");
preparedStatement.setString(1,"%"+sname+"%");
List<Sort> sortList = new ArrayList<>();
ResultSet resultSet = preparedStatement.executeQuery();
Sort sort = null;
while(resultSet.next() == true)
{
int sid = resultSet.getInt("sid");
String sname1 = resultSet.getString("sname");
String username = resultSet.getString("username");
Integer password = resultSet.getInt("password");
Date birthday = resultSet.getTimestamp("birthday");
String mail = resultSet.getString("mail");
Integer role = resultSet.getInt("role");
Sort sort1 = new Sort();
sort1.setSid(sid);
sort1.setUsername(username);
sort1.setSname(sname1);
sort1.setPassword(password);
sort1.setBirthday(birthday);
sort1.setMail(mail);
sort1.setRole(role);
sortList.add(sort1);
}
// JDBCUtils.close(resultSet,preparedStatement,connection);
return sortList;
}
public static List<Sort> getAllSorts() throws SQLException {
Connection connection = JDBCUtils.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("select * from sort");
List<Sort> sortList = new ArrayList<>();
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next() == true)
{
int sid = resultSet.getInt("sid");
String sname = resultSet.getString("sname");
String username = resultSet.getString("username");
Integer password = resultSet.getInt("password");
Date birthday = resultSet.getTimestamp("birthday");
String mail = resultSet.getString("mail");
Integer role = resultSet.getInt("role");
Sort sort = new Sort();
sort.setSid(sid);
sort.setUsername(username);
sort.setSname(sname);
sort.setPassword(password);
sort.setBirthday(birthday);
sort.setMail(mail);
sort.setRole(role);
sortList.add(sort);
}
// JDBCUtils.close(resultSet,preparedStatement,connection);
return sortList;
}
// public static void main(String[] args) throws SQLException {
// System.out.println("查一個");
// Sort sort = getSortByID(11);
// if(sort != null)
// {
// System.out.println(sort);
// }
// else
// {
// System.out.println("沒查到");
// }
//
// System.out.println("查全部");
// List<Sort> sortList = getAllSorts();
// for(Sort sort1:sortList)
// {
// System.out.println(sort1);
// }
}
人事管理系統(tǒng) 增刪改查 方法
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門浪腐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人顿乒,你說我怎么就攤上這事议街。” “怎么了璧榄?”我有些...
- 文/不壞的土叔 我叫張陵特漩,是天一觀的道長。 經(jīng)常有香客問我骨杂,道長涂身,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任腊脱,我火速辦了婚禮访得,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘陕凹。我一直安慰自己悍抑,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布杜耙。 她就那樣靜靜地躺著搜骡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪佑女。 梳的紋絲不亂的頭發(fā)上记靡,一...
- 文/蒼蘭香墨 我猛地睜開眼紊选,長吁一口氣:“原來是場噩夢啊……” “哼啼止!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起兵罢,我...
- 正文 年R本政府宣布抛蚁,位于F島的核電站,受9級特大地震影響惕橙,放射性物質(zhì)發(fā)生泄漏瞧甩。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一弥鹦、第九天 我趴在偏房一處隱蔽的房頂上張望肚逸。 院中可真熱鬧,春花似錦彬坏、人聲如沸朦促。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽务冕。三九已至,卻和暖如春幻赚,著一層夾襖步出監(jiān)牢的瞬間禀忆,已是汗流浹背。 一陣腳步聲響...