新聞中心
JDBC例子1,首先在配置文件(system.properties)中配置上如下內(nèi)容:

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比玄武網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式玄武網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務覆蓋玄武地區(qū)。費用合理售后完善,十載實體公司更值得信賴。
- driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
- url=jdbc:sqlserver://localhost:1433;databaseName=mp
- username=sa
- password=mengya
JDBC例子2,寫了個SQLDB的工具類
- publicclassSQLDBConnection{
- privateInputStreaminputstr;
- privatePropertiespro;
- privatestaticSQLDBConnectionsqldb=null;
- //私有構(gòu)造方法
privateSQLDBConnection(){
inputstr=this.getClass().getResourceAsStream("/system.properties");
pro=newProperties();
try{
pro.load(inputstr);
}catch(IOExceptione){
e.printStackTrace();
}
try{
Class.forName(pro.getProperty("driver"));//注冊驅(qū)動,只注冊一次
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
}
//單例模式
publicstaticSQLDBConnectiongetSQLDBConnection(){
if(sqldb==null){
synchronized(SQLDBConnection.class){
if(sqldb==null){
sqldb=newSQLDBConnection();
}
}
}
returnsqldb;
}
//得到與數(shù)據(jù)庫的連接
publicConnectionGetConnection(){
Connectionconn=null;
try{
conn=DriverManager.getConnection(pro.getProperty("url"),pro.getProperty("username"),pro.getProperty("password"));
}catch(SQLExceptione){
e.printStackTrace();
}
returnconn;
}
JDBC例子3,寫好Studao的接口
- //釋放資源
- publicstaticvoidfree(ResultSetrs,Statementsta,Connectionconn){
- try{
- if(rs!=null){
- rs.close();
- }
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- try{
- if(sta!=null){
- sta.close();
- }
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- if(conn!=null){
- try{
- conn.close();
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- }
- }
- }
- }
- publicinterfaceStudaointer{
- publicvoidaddStu(Stustu);
- publicvoiddelStu(intsid);
- publicvoidupdStu(Stustu);
- publicStugetOneStu(intsid);
- publicListgetAllStu();
- }
JDBC例子4,寫好自己定義的RuntimeException
- publicclassMySQLExceptionextendsRuntimeException{
- privatestaticfinallongserialVersionUID=1L;
- }
JDBC例子5,寫好Studao的實現(xiàn)類
- publicclassStuDAOImpleimplementsStudaointer{
- privateConnectionconn;
- privatePreparedStatementpre;
- privateResultSetrs;
- publicvoidaddStu(Stustu){
- Stringsql="insertintostuvalues(?,?,?)";
- conn=SQLDBConnection.getSQLDBConnection().GetConnection();
- try{
- pre=conn.prepareStatement(sql);
- pre.setString(1,stu.getSname());
- pre.setString(2,stu.getSsex());
- pre.setDate(3,newjava.sql.Date(stu.getSbrith().getTime()));
- pre.executeUpdate();
- }catch(SQLExceptione){
- e.printStackTrace();
- thrownewMySQLException();//異常向上拋
- }finally{
- SQLDBConnection.free(rs,pre,conn);
- }
- }
- publicvoiddelStu(intsid){
- Stringsql="deletestuwheres_id=?";
- conn=SQLDBConnection.getSQLDBConnection().GetConnection();
- try{
- pre=conn.prepareStatement(sql);
- pre.setInt(1,sid);
- pre.executeUpdate();
- }catch(SQLExceptione){
- e.printStackTrace();
- thrownewMySQLException();
- }finally{
- SQLDBConnection.free(rs,pre,conn);
標題名稱:淺析一個簡單的JDBC例子
轉(zhuǎn)載源于:http://www.dlmjj.cn/article/djdjhho.html


咨詢
建站咨詢
