mysql資料庫的串連以及增刪改查Java代碼實現(PreparedStatement版),mysql資料庫增刪改查

來源:互聯網
上載者:User

mysql資料庫的串連以及增刪改查Java代碼實現(PreparedStatement版),mysql資料庫增刪改查

資料庫:

create table t1(id int primary key not null auto_increment,name varchar(32),password varchar(32));insert into t1(name,password) values('admin','123');insert into t1(name,password) values('zhangsan','123');insert into t1(name,password) values('lisi','123');

Java代碼:


mySqlConnection.java代碼:

package com.dbdao.mysql;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;public class mySqlConnection {//建立資料庫驅動名稱private static String Driver_class="com.mysql.jdbc.Driver";//資料庫連結地址private String url="jdbc:mysql://localhost:3306/test";//資料庫使用者名稱private String user="root";//資料庫密碼private String password="11";//資料庫連結private Connection con=null;//準備聲明sql語句private PreparedStatement pstmt=null;//結果集private ResultSet rs=null;//影響行數private int i;/* * 建立驅動 * */static{try {Class.forName(Driver_class);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/* * 載入驅動 * */@Testpublic void getConnect() {// TODO Auto-generated method stubtry {con=DriverManager.getConnection(url, user, password);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//判斷資料庫是否載入成功if(con!=null){System.out.println("資料庫載入成功!");}else{System.out.println("資料庫載入失敗!");}}/* * 執行sql語句 * */public void doSql(String sql,Object[] object) {// TODO Auto-generated method stub//判斷sql語句是否存在if(sql!=null){//載入驅動getConnect();//判斷object數組是否存在if(object==null){//如果不存在,建立一個,防止出現null 指標異常object=new Object[0];}try {//聲明一條準備的sql語句pstmt=con.prepareStatement(sql);//為Object對象一一賦值for(int i=0;i<object.length;i++){pstmt.setObject(i+1, object[i]);}//執行聲明的sql語句pstmt.execute();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else{System.out.println("sql語句並不存在!");}}/* * 擷取結果集 * */public ResultSet getRS(){try {//擷取結果集方法rs=pstmt.getResultSet();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//返回結果集return rs;}/* * 擷取影響行數 * */public int getUpdateCount() {// TODO Auto-generated method stubtry {//擷取影響行數方法i=pstmt.getUpdateCount();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//返回影響行數return i;}/* * 關閉方法 * */public void getClose() {// TODO Auto-generated method stub//關閉結果集try {//結果集關閉方法rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//關閉聲明的sql語句try {//關閉聲明的sql語句方法pstmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//卸載驅動try {//驅動卸載方法con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

loginService.java代碼:

package com.service;import java.sql.ResultSet;import java.sql.SQLException;import org.junit.Test;import com.dbdao.mysql.mySqlConnection;public class loginService {//匯入mySqlConnection類mySqlConnection mysqlCon=new mySqlConnection();//準備sql語句private String sql;//影響行數(資料變更後,影響行數都是大於0,等於0時沒變更,所以說如果變更失敗,那麼影響行數必定為負)private int i=-1;//結果集private ResultSet rs;/* * 插入資料 * */@Testpublic void insert() {// TODO Auto-generated method stub//建立sql語句sql="insert into t1(name,password) values(?,?)";//建立object數組Object[] object=new Object[]{"admin","123456"};//執行sql語句mysqlCon.doSql(sql, object);//擷取影響行數i=mysqlCon.getUpdateCount();//判斷是否插入成功if(i!=-1){System.out.println("資料插入成功!");}else{System.out.println("資料插入失敗!");}//關閉連結mysqlCon.getClose();}/* * 刪除資料 * */@Testpublic void delete() {// TODO Auto-generated method stub//建立sql語句sql="delete from t1 where id=?";//建立object數組Object[] object=new Object[]{3};//執行sql語句mysqlCon.doSql(sql, object);//擷取影響行數i=mysqlCon.getUpdateCount();//判斷是否刪除成功if(i!=-1){System.out.println("資料刪除成功!");}else{System.out.println("資料刪除失敗!");}//關閉連結mysqlCon.getClose();}/* * 更新資料 * */@Testpublic void update() {// TODO Auto-generated method stub//建立sql語句sql="update t1 set password=? where name=?";//建立Object數組Object[] object=new Object[]{"11","admin"};//執行sql語句mysqlCon.doSql(sql, object);//擷取影響行數i=mysqlCon.getUpdateCount();//判斷資料是否更新成功if(i!=-1){System.out.println("資料更新成功!");}else{System.out.println("資料更新失敗!");}//關閉連結mysqlCon.getClose();}/* * 遍曆資料 * */@Testpublic void select() {// TODO Auto-generated method stub//建立sql語句sql="select * from t1";//執行sql語句mysqlCon.doSql(sql, null);//擷取結果集rs=mysqlCon.getRS();//判斷結果集是否為空白if(rs!=null){try {//將游標移動到結果集末端,注意此處不能使用rs.afterLast();否則為空白值。rs.last();//擷取結果集行數i=rs.getRow();//判斷結果集是否存在if(i>0){//將游標移動到結果集前端rs.beforeFirst();//迴圈遍曆所有行數while(rs.next()){//遍曆每行元素的內容String name=rs.getString("name");String password=rs.getString("password");//在控制台列印出結果System.out.println("name:"+name+"   password:"+password);}}else{System.out.println("結果集為空白!");}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else{System.out.println("結果集不存在!");}//關閉連結mysqlCon.getClose();}}




相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.