您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > java中用配置文件连接数据库
ÓÃÅäÖÃÎļþÁ¬½ÓÊý¾Ý¿â1.EclipseÖУ¬ÔÚsrcĿ¼Ï½¨Á¢db.propertiesÎļþ£¬ÔÚÀïÃæÅäÖÃÊý¾Ý¿âÁ¬½ÓËùÐèµÄDriver,url,user,possword£¬×¢ÒâµÈºÅ×óÓÒ²»ÄܿոñÈ磺driver=com.microsoft.sqlserver.jdbc.SQLServerDriverurl=jdbc:sqlserver://localhost:1433;databaseName=restrantuser=sapassword=1102.дһ¸öÊý¾Ý¿âÁ¬½ÓÀàimportjava.io.IOException;importjava.sql.*;importjava.util.Properties;publicConnectiongetCon(){Connectioncon=null;Propertiesproperties=newProperties();try{properties.load(this.getClass().getResourceAsStream(/db.properties));Stringdriver=properties.getProperty(driver);Stringurl=properties.getProperty(url);Stringuser=properties.getProperty(user);Stringpassword=properties.getProperty(password);try{Class.forName(driver);con=DriverManager.getConnection(url,user,password);}catch(ClassNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(SQLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returncon;}
三七文档所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
本文标题:java中用配置文件连接数据库
链接地址:https://www.777doc.com/doc-1796589 .html