caoporm97国产在线视频|欧美性XXXXX精品|一本一道久久a久久精品综合开|精品久久久久久久久久久AⅤ|

十年專注于品牌網(wǎng)站建設(shè) 十余年專注于網(wǎng)站建設(shè)_小程序開發(fā)_APP開發(fā),低調(diào)、敢創(chuàng)新、有情懷!
南昌百恒網(wǎng)絡(luò)微信公眾號(hào) 掃一掃關(guān)注
小程序
tel-icon全國服務(wù)熱線:400-680-9298,0791-88117053
掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號(hào)
掃一掃打開百恒網(wǎng)絡(luò)微信小程序

百恒網(wǎng)絡(luò)

南昌百恒網(wǎng)絡(luò)

java連接mysql實(shí)現(xiàn)數(shù)據(jù)增加、刪除、修改、查詢操作

百恒網(wǎng)絡(luò) 2014-09-23 8327

任何程序的實(shí)戰(zhàn)應(yīng)用都是與數(shù)據(jù)庫有關(guān),因?yàn)樵谟脩舨僮鬟^程中肯定有互動(dòng),有互動(dòng)意味著有數(shù)據(jù)的讀取,更新等操作,接下來由百恒科技工程師向大介紹java連接mysql實(shí)現(xiàn)數(shù)據(jù)增加、刪除、修改、查詢的基本操作.

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

//寫一個(gè)通用類,操作數(shù)據(jù)庫,即我們通常執(zhí)行sql語句

public class OperateDB{

public static ResultSet queryDB(Statement sm,String sql){

ResultSet rs= null;

try{

rs=sm.executeQuery(sql);

} catch (SQLException e){e.printStackTrace();}

return rs;

}

public static int updateDB(Connection con ,String sql){

Statement sm=null;

int affectRows=0;

try {sm=con.createStatement();

affectRows=sm.executeUpdate(sql);

}catch(SQLException e){

e.printStackTrace();

affectRows=Statement.EXECUTE_FAILED;}

finally {closeStatement(sm);}

return affectRows;

}

public static void showResultSet(ResultSet rs){

if (rs==null){return;}

try{ResultSetMetaData md=rs.getMetaData();

int columnCount=md.getColumnCount();

if (!rs.isBeforeFirst()){rs.beforeFirst();}

while(rs.next()){

for (int i=1;i System.out.print(rs.getObject(i)+"t");

}

System.out.print(rs.getObject(columnCount)+"rn");

}

rs.beforeFirst();

}catch(SQLException e){e.printStackTrace();}

}

public static void closeStatement(Statement sm){

if(sm!=null){

try{

sm.close();

}catch (SQLException el){el.printStackTrace();}

}

}

public static void closeConnection(Connection con){

if(con !=null){

try{

con.close();

}catch (SQLException el){el.printStackTrace();}

}

}

public static void main(String[] args)throws ClassNotFoundException,SQLException{

String dbName="test";

String userName="root";

String password="123456";

String querySQL="SELECT *FROM student";

String updateSQL="UPDATE student SET EnglishObj=100 where UserName='百恒科技'";

String insertSQL="INSERT INTO student(UserName,UserSex,ChineseObj) values('劉工','男','88')";

//String deleteSQL="Delete from student where UserName='劉工'";

Connection con=null;

Statement sm=null;

try{

con=DBConnector.getMySQLConnection(null,null,null,dbName,userName,password);

sm=con.createStatement();

ResultSet rs=OperateDB.queryDB(sm, querySQL);

System.out.println("修改數(shù)據(jù)表之前的數(shù)據(jù)");

OperateDB.showResultSet(rs);

OperateDB.updateDB(con,updateSQL);

OperateDB.updateDB(con, insertSQL);

//OperateDB.updateDB(con, deleteSQL);

System.out.println();

System.out.println("修改數(shù)據(jù)表之后的數(shù)據(jù)");

rs=OperateDB.queryDB(sm, querySQL);

OperateDB.showResultSet(rs);

System.out.println();

rs.absolute(1);

System.out.print("UserName:" +rs.getString("UserName")+"t");

System.out.println("EnblishObj"+rs.getInt("EnglishObj"));

rs.absolute(3);

System.out.print("UserName:" +rs.getString("UserName")+"t");

System.out.println("EnblishObj"+rs.getInt("EnglishObj"));

rs.close();

}catch (ClassNotFoundException el){

throw el;

}catch (SQLException e2){

throw e2;

}finally{

closeStatement(sm);

closeConnection(con);

}

}

}

原創(chuàng)文章出自:百恒網(wǎng)絡(luò) 如轉(zhuǎn)載請(qǐng)注明出處!專業(yè),專注于南昌網(wǎng)站建設(shè),為用戶提供高品質(zhì)的網(wǎng)絡(luò)產(chǎn)品及優(yōu)質(zhì)服務(wù)是我們始終的追求.

400-680-9298,0791-88117053
掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號(hào)
掃一掃打開百恒網(wǎng)絡(luò)小程序

歡迎您的光顧,我們將竭誠為您服務(wù)×

售前咨詢 售前咨詢
 
售前咨詢 售前咨詢
 
售前咨詢 售前咨詢
 
售前咨詢 售前咨詢
 
售前咨詢 售前咨詢
 
售后服務(wù) 售后服務(wù)
 
售后服務(wù) 售后服務(wù)
 
備案專線 備案專線
 
×