解决方法:1.连接url格式出现了问题(Connection conn = DriverManager.getConnection (“jdbc:mysql://localhost:3306/数据库名”,“root”,“密码”)
2.驱动字符串出错(com.mysql.jdbc.Driver)
3.Classpath中没有加入合适的mysql_jdbc驱动
4.没有mysql-connector-java-3.1.14-bin.jar的jar包,或者路径出错
检查是否有启动驱动程序db.properties配置文件如下:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/data?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456(注意密码不能错)
或者是以下情况:
修改前的代码如下所示:package cn.qjq.jdbc; import java.io.IOException; import java.io.InputStream; 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 class JDBCUtils { private static String driver; private static String url; private static String username; private static String password; //静态语句块 static { //JdBCUtils.class获得对象 //getClassloader()类加载器 //getResourceAsStream("db.properties")加载资源文件放到输入流中 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"); //创建Properties类型的对象 Properties p = new Properties(); //加载流文件 try { p.load(is); driver = p.getProperty("driver"); url = p.getProperty("url"); username = p.getProperty("username"); password = p.getProperty("password "); //加载MySQL驱动 Class.forName(driver); System.out.println("驱动成功"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //获得连接对象的方法 public static Connection getConnection() { try { System.out.print("数据库连接成功"); return DriverManager.getConnection(url,username,password); }catch (Exception e) { System.out.print("数据库连接失败"); e.printStackTrace(); } return null; } }修改后的代码如下:
package cn.qjq.jdbc; import java.io.IOException; import java.io.InputStream; 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 class JDBCUtils { private static String driver; private static String url; private static String username; private static String password; // 静态语句块 static { try { // getResourceAsStream("db.properties") 获取资源 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(is); driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); // 驱动只加载一次 Class.forName(driver); } catch (Exception e) { e.printStackTrace(); } } // 获取连接的方法 public static Connection getConnection() throws SQLException { Connection conn = DriverManager.getConnection(url, username, password); return conn; } // 释放连接资源的方法 public static void release(Connection conn, Statement st, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }结果展示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)