81,094
社区成员
发帖
与我相关
我的任务
分享
try {
Class.forName(dv);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
看一下,mysql驱动有没有放置到类路径下面
package webwork;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Statement;
public class JDBCUtil {
private static String url = null;
private static String user = null;
private static String password = null;
private static String dv = null;
static {
Properties properties = new Properties();
InputStream in = JDBCUtil.class.getResourceAsStream("/a.properties");
try {
properties.load(in);
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
dv = properties.getProperty("driver");
} catch (IOException e) {
e.printStackTrace();
}
try {
Class.forName(dv);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection connection() throws SQLException {
Connection connection = DriverManager
.getConnection(url, user, password);
return connection;
}
public static void close(Statement statement, Connection connection) {
if (statement != null)
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement,
Connection connection, ResultSet resultSet) {
if (preparedStatement != null)
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (resultSet != null)
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}