java.lang.NoClassDefFoundError: Could not initialize class com.purplesky.jdbc.utils.JDBCUtils
工具类
public class JDBCUtils {
//定义属性
private static String user;
private static String password;
private static String url;
private static String driver;
//在静态代码块初始化
static {
try {
Properties properties = new Properties();
properties.load(new FileInputStream("src:\mysql.properties"));
user = properties.getProperty("user");
password = properties.getProperty("password");
url = properties.getProperty("url");
driver = properties.getProperty("driver");
Class.forName(driver);
} catch (Exception e) {
//开发中,可以这样
//1.将编译异常转成运行异常
//2.调用者,可以选择捕获异常,也可以选择默认处理该异常,比较方便
throw new RuntimeException(e);
}
}
//连接数据库
public static Connection getConnection() {
try {
return DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//关闭
public static void close(ResultSet set, Statement statement,Connection connection) {
try {
if (set != null) {
set.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
测试类
@Test
public void testDML() {
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "update actor set name = ? where id =?";
try {
connection = JDBCUtils.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "周星驰");
preparedStatement.setInt(2, 2);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null, preparedStatement, connection);
}
}
问题描述
例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据
APP 中接收数据代码:
@Override
public void run() {
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(READ_DATA, bytes, -1, buffer).sendToTarget();
}
原因分析:
- 先跑了下sql发现正常运行
- 然后检查了配置文件
检查了配置文件的确在src下,使用的也是绝对路径
仔细一看路径多了个 :
properties.load(new FileInputStream("src:\mysql.properties"));
去掉后即可正常运行
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)