如何进行JDBC的实例分析

发布时间:2021-12-18 20:34:36 作者:柒染
来源:亿速云 阅读:127

如何进行JDBC的实例分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

JDBC例子1,首先在配置文件(system.properties)中配置上如下内容:

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;//私有构造方法privateSQLDBConnection(){inputstr=this.getClass().getResourceAsStream("/system.properties");pro=newProperties();try{pro.load(inputstr);}catch(IOExceptione){e.printStackTrace();}try{Class.forName(pro.getProperty("driver"));//注册驱动,只注册一次}catch(ClassNotFoundExceptione){e.printStackTrace();}}//单例模式publicstaticSQLDBConnectiongetSQLDBConnection(){if(sqldb==null){synchronized(SQLDBConnection.class){if(sqldb==null){sqldb=newSQLDBConnection();}}}returnsqldb;}//得到与数据库的连接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的实现类

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);

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 使用JDBC怎么对ORACLE进行连接
  2. 怎么对JDBC进行封装

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jdbc

上一篇:如何解析Swing线程的内容

下一篇:如何进行springboot配置templates直接访问的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》