/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述: * * 历史版本: JDK 14.02 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 mysql-connector-java-8.0.27.jar. * 数据库:MySQL Server 8.0 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuMysqlCon.java * * */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.*; public class DuMysqlCon { Connection con; Statement stmt = null; // public Connection getConnection() { try { Class.forName("com.mysql.cj.jdbc.Driver"); //com.mysql.cj.jdbc.Driver com.mysql.jdbc.Driver访问需要相应的JAR版本,在安装的数据库下拷贝来引用 System.out.println("数据库驱动加载成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8", "root", "7涂聚文"); System.out.println("数据库连接成功"); System.out.println(" 实例化Statement对象..."); // 执行查询 stmt = con.createStatement(); String sql; sql = "SELECT city_id,city,country_id FROM city"; ResultSet rs = stmt.executeQuery(sql); // 展开结果集数据库 while(rs.next()){ // 通过字段检索 int cityid = rs.getInt("city_id"); String city = rs.getString("city"); int countryid = rs.getInt("country_id"); // 输出数据 System.out.print("ID: " + cityid); System.out.print(" , 城市名字: " + city); System.out.print(" , 区域cid: " + countryid); System.out.print("\n"); } // 完成后关闭 rs.close(); stmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } return con; } }
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述: * * 历史版本: JDK 14.02 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 mysql-connector-java-8.0.27.jar * 数据库:MySQL Server 8.0 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuCon.java * * */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import com.mysql.cj.*; import com.mysql.jdbc.*; import java.io.*; import java.lang.*; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.io.InputStreamReader; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.sql.*; public class DuCon { Connection con; //private static Properties duproperties; private static Properties pp = null; private static InputStream fis = null; private static String url = ""; private static String driverName=""; private static String user=""; private static String password =""; public Connection getConnection() { // duproperties =new Properties();//创建资源对象 pp = new Properties(); try { fis=DuCon.class.getClassLoader().getResourceAsStream("dbinfo.properties"); pp.load(fis); url = pp.getProperty("url"); driverName = pp.getProperty("driverName"); user = pp.getProperty("user"); password = pp.getProperty("password"); } catch(Exception e) { e.printStackTrace(); } //properties.load(new InputStreamReader(DuCon.class.getClassLoader().getResourceAsStream("dbinfo.properties"),"UTF-8")); try { Class.forName(driverName); System.out.println("数据库驱动加载成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection(url,user,password); System.out.println("数据库连接成功"); } catch (SQLException e) { e.printStackTrace(); } return con; } }
dbinfo.properties:(放在SRC文夹下)
driverName=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/sakila user=root password=涂聚文
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述: * * 历史版本: JDK 14.02 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuMain.java * * */ import com.mysql.cj.*; import com.mysql.jdbc.*; // import com.intellij.database.*; // import com.intellij.database.dialects.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DuMain { public static void main(String[] args) { DuMysqlCon c = new DuMysqlCon(); c.getConnection(); DuCon cn=new DuCon(); cn.getConnection(); } }
IntelliJ IDEA 2021.2.3 ,Eclipse IDE for Enterprise Java and Web Developers - 2021-09