连接
数据库的
代码
public class
DatabaseCo
nnect {
private Co
nnection conn;
public
DatabaseCo
nnect() throws ClassNotFoundException, SQLException{
Class.forName("com.
mysql.jdbc.Driver");
System.out.println("load ok");
String url = "jdbc:
mysql://localhost:3306/
MySQL?user=root&password;=";
conn = DriverManager.getCo
nnection(url);
System.out.println("co
nnect ok");
}
public static void main(String[] args) {
try {
new
DatabaseCo
nnect();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet runQuery(String sql) throws SQLException {
Statement statement = conn.createStatement();
ResultSet records = statement.executeQuery(sql);
return records;
}
public void excuteQuery(String sql) throws SQLException {
Statement statement = conn.createStatement();
statement.execute(sql);
}
public void close() throws SQLException{
conn.close();
}