任何程序的實(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ù)是我們始終的追求.