CURD
introduce一下jdbc中的CURD,源碼如下:
(一):首先一個Javabeen
(二):jdbc工具類:
package Practise;import static org.hamcrest.CoreMatchers.nullValue; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;/*** jdbc工具類:* @author 曹**/ public class JdbcUtil {//驅動類名稱private static String driver="java.sql.DriverManager";//數據庫的路徑jdbc:mysql://137.0.0.1:3306/數據庫名稱private static String url="jdbc:mysql://127.0.0.1:3306/blog";//數據庫用戶名private static String userName="root";//數據庫密碼private static String password="root";/*** 類加載時加載驅動,高版本可以省略*/static {try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}/*** 創建連接并且獲取連接* @return:返回一個數據庫連接對象*/public static Connection conn() {Connection conn=null;try {conn=DriverManager.getConnection(url, userName, password);} catch (SQLException e) {e.printStackTrace();}return conn;}/*** 數據庫關閉* @param conn:連接對象* @param st:傳輸數據集* @param rs:返回結果集*/public static void close(Connection conn,Statement st,ResultSet rs) {if(conn!=null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}else {conn=null;}if(st!=null) {try {st.close();} catch (SQLException e) {e.printStackTrace();}}else {st=null;}if(rs!=null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}else {rs=null;}}/*** 數據庫關閉* @param conn:連接對象* @param st:傳輸數據集*/public static void close(Connection conn,Statement st) {if(conn!=null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}else {conn=null;}if(st!=null) {try {st.close();} catch (SQLException e) {e.printStackTrace();}}else {st=null;}} }(三):CURD:插入
package Practise; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class JdbcPractise {public static void main(String[] args) {Student stu = new Student();stu.setSno(5);stu.setSname("陸文軒");stu.setAge(30);stu.setSex("女");int result=add(stu);if(result>0) {System.out.println("添加成功");}else {System.out.println("添加失敗");}}/*** 插入:* @param stu* @return*/public static int add(Student stu) {Connection conn=JdbcUtil.conn();Statement st = null;int result=0;try {st=conn.createStatement();result = st.executeUpdate("insert into student(sno,sname,age,sex) values("+stu.getSno()+",'"+stu.getSname()+"',"+stu.getAge()+",'"+stu.getSex()+"')");} catch (SQLException e) {e.printStackTrace();}finally {JdbcUtil.close(conn, st);}return result;}}(四):CURD:刪除
package Practise; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class JdbcPractise {public static void main(String[] args) {int result=delete(5);if(result>0) {System.out.println("刪除成功");}else {System.out.println("刪除失敗");}}/*** 刪除:根據學號刪除* @param id:學號* @return:返回受影響行數*/public static int delete(int id) {Connection conn=JdbcUtil.conn();Statement st=null;int result=0;try {st=conn.createStatement();result=st.executeUpdate("delete from student where sno="+id+"");} catch (SQLException e) {e.printStackTrace();}finally {JdbcUtil.close(conn, st);}return result;} }(五):CURD:修改
package Practise; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; public class JdbcPractise {public static void main(String[] args) {Student stu=new Student();stu.setSno(2);stu.setSname("劉麗麗");stu.setAge(25);int result=update(stu);if(result>0) {System.out.println("修改成功");}else {System.out.println("修改失敗");}}/*** 修改:* @param stu:要修改表對象* @return:返回受影響行數*/public static int update(Student stu) {Connection conn=JdbcUtil.conn();Statement st=null;int result=0;try {st=conn.createStatement();result=st.executeUpdate("update student set sname='"+stu.getSname()+"',age="+stu.getAge()+" where sno="+stu.getSno()+"");} catch (SQLException e) {e.printStackTrace();}return result;} }(六):CURD:查詢
package Practise; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcPractise {public static void main(String[] args) {Student stu = select(2);if(stu!=null) {System.out.println(stu.toString());}else {System.out.println("沒有符合的結果集");}}/*** 按照sno查詢 * @param id:需要查詢的sno* @return:返回一個Student表對象*/public static Student select(int id) {Connection conn=JdbcUtil.conn();Statement st=null;ResultSet rs=null;Student stu=null;try {st=conn.createStatement();rs = st.executeQuery("select * from student where sno="+id+"");while(rs.next()) {stu=new Student();String sname = rs.getString("sname");int age = rs.getInt("age");String sex = rs.getString("sex");stu.setSno(id);stu.setSname(sname);stu.setAge(age);stu.setSex(sex);}} catch (SQLException e) {e.printStackTrace();}finally {JdbcUtil.close(conn, st, rs);}return stu;} }(七)CURD:查詢所有
package Practise; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class JdbcPractise {public static void main(String[] args) {List<Student> list = selectAll();if(list.size()>0) {System.out.println(list);}else {System.out.println("沒有數據");}}/*** 查詢所有:* @return:返回一個list集合*/public static List<Student> selectAll(){List<Student> list = new ArrayList<Student>();Connection conn=JdbcUtil.conn();Statement st = null;ResultSet rs = null;Student stu=null;try {st=conn.createStatement();rs=st.executeQuery("select * from student");while(rs.next()) {stu=new Student();int sno = rs.getInt("sno");String sname = rs.getString("sname");int age=rs.getInt("age");String sex = rs.getString("sex");stu.setSno(sno);stu.setSname(sname);stu.setAge(age);stu.setSex(sex);list.add(stu);}} catch (SQLException e) {e.printStackTrace();}finally {JdbcUtil.close(conn, st, rs);}return list;} }總結
- 上一篇: 百度地图POI的边界GEOJSON数据采
- 下一篇: 用jQuery写开关灯案例