欢迎来到天天文库
浏览记录
ID:51700935
大小:36.95 KB
页数:2页
时间:2020-03-15
《各种数据库的连接连接Connection方法.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、packagecom.jdbc.myConnection;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;publicclassConnectionFactory{//获取Oracle连接publicstaticConnectiongetOracleConnection(){Connectioncon=null;try{Class.forName("oracle.jdbc.OracleDrive
2、r");Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";con=DriverManager.getConnection(url,"scott","tiger");}catch(Exceptione){e.printStackTrace();}returncon;}//获取SQLServer的test数据库连接publicstaticConnectiongetSQLServerConnection(){returngetMySQLConnection("test
3、");}//获取SQLServer连接publicstaticConnectiongetSQLServerConnection(StringdatabaseName){Connectioncon=null;try{Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");Stringurl="jdbc:microsoft:sqlServer://localhost:1433;databaseName="+databaseName;con=Driv
4、erManager.getConnection(url,"sa","sa");}catch(Exceptione){e.printStackTrace();}returncon;}//获取MySQL连接publicstaticConnectiongetMySQLConnection(){returngetMySQLConnection("test");}publicstaticConnectiongetMySQLConnection(StringdatabaseName){Connectioncon=null;
5、try{Class.forName("com.mysql.jdbc.Driver");Stringurl="jdbc:mysql://localhost/"+databaseName;con=DriverManager.getConnection(url,"root","root");}catch(Exceptione){e.printStackTrace();}returncon;}//关闭连接publicstaticvoidclose(Connectioncon){try{con.close();}catc
6、h(SQLExceptione){e.printStackTrace();}}}
此文档下载收益归作者所有