jdbc
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc相关的知识,希望对你有一定的参考价值。
package controllers.CarSecurityCheckPlugin.util; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public final class ConnUtils { private static String driverName; private static String dbURL; private static String userName; private static String userPwd; private ConnUtils() { } static { // driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; // dbURL="jdbc:sqlserver://192.168.20.245:1433;DatabaseName=anjianduijie"; // userName="sa"; // userPwd="123"; Properties pro = new Properties(); try { pro.load(new FileInputStream("modules/CarSecurityCheckPlugin/conf/jdbc.properties")); } catch (IOException e1) { e1.printStackTrace(); } driverName = pro.getProperty("jdbc.driverName"); dbURL = pro.getProperty("jdbc.dbURL"); userName = pro.getProperty("jdbc.userName"); userPwd = pro.getProperty("jdbc.userPwd"); try { Class.forName(driverName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() { try { Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd); return dbConn; } catch (SQLException e) { e.printStackTrace(); } return null; } /** * 释放资源 * * @param rs * @param st * @param conn */ public static void free(ResultSet rs, Statement st, Connection conn) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
jdbc.properties文件里面写
jdbc.driverName=com.microsoft.sqlserver.jdbc.SQLServerDriver这些配置。
以上是关于jdbc的主要内容,如果未能解决你的问题,请参考以下文章