zhenghao111
7/22/2012 - 12:20 PM

java:mysql连接代码

java:mysql连接代码

/*database.properties*/
jdbc.drivers=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/corejava
jdbc.username=root
jdbc.password=159753

/*mysql连接代码*/
  public static Connection getConnection() throws SQLException, IOException
   {
      Properties props = new Properties();
      FileInputStream in = new FileInputStream("database.properties");
      props.load(in);
      in.close();

      String drivers = props.getProperty("jdbc.drivers");
      if (drivers != null) System.setProperty("jdbc.drivers", drivers);
      String url = props.getProperty("jdbc.url");
      String username = props.getProperty("jdbc.username");
      String password = props.getProperty("jdbc.password");

      return DriverManager.getConnection(url, username, password);
   }