packageJDBCDemo;2
3 importjava.io.FileInputStream;4 importjava.io.FileNotFoundException;5 importjava.io.IOException;6 importjava.io.InputStream;7 importjava.sql.DriverManager;8 importjava.sql.PreparedStatement;9 importjava.sql.ResultSet;10 importjava.sql.SQLException;11 importjava.util.InputMismatchException;12 importjava.util.Properties;13
14 importcom.mysql.jdbc.Connection;15 importcom.mysql.jdbc.Statement;16
17 public classJDBCUtils {18 private voidJDBCUtils() {};19 private static String sqlurl = "";20 private static String sqluser = "";21 private static String sqlpwd = "";22 private static String classforName = "";23 private static Connection con = null;24
25 static{26 try{27 initConfig();28 Class.forName(classforName);29 con =(Connection) DriverManager.getConnection(sqlurl, sqluser, sqlpwd);30 } catch(ClassNotFoundException e) {31 System.out.println("数据驱动失败");32 e.printStackTrace();33 } catch(SQLException e) {34
35 System.out.println("数据连接失败");36 e.printStackTrace();37 }38 }39
40 private static voidinitConfig() {41 InputStream inf = JDBCUtils.class.getClassLoader().getResourceAsStream("database.propertties");42 Properties properties = newProperties();43 try{44 properties.load(inf);45 classforName = properties.getProperty("driverClass");46 sqlurl = properties.getProperty("url");47 sqluser = properties.getProperty("username");48 sqlpwd = properties.getProperty("password");49 } catch(IOException e) {50 System.out.println("读取配置文件失败");51 }52 }53
54 public staticConnection getConnection() {55 returncon;56 }57 public static voidclose(Connection con,Statement preparedStatement,ResultSet rs) {58 if(con !=null) {59 try{60 con.close();61 } catch(SQLException e) {62 //e.printStackTrace();
63 }64
65 }66 if (preparedStatement!=null) {67 try{68 preparedStatement.close();69 } catch(Exception e) {70 //e.printStackTrace();
71 }72 }73 if (rs!=null) {74 try{75 rs.close();76 } catch(Exception e) {77 //e.printStackTrace();
78 }79 }80 }81 }
java-JDBC.mysql 工具类 读取本地文件配置
标签:exce dstat res getc catch imp 失败 util post
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:http://www.cnblogs.com/wwcherish/p/8037395.html