-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB.java
45 lines (40 loc) · 1.21 KB
/
DB.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
/**
* Write a description of DB here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class DB {
public Connection conn = null;
public DB(){
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/Crawler";
conn = DriverManager.getConnection(url,"root","admin213");
System.out.println("Connection has been established");
} catch (SQLException e) {
e.printStackTrace();
} catch ( ClassNotFoundException e){
e.printStackTrace();
}
}
public ResultSet runSql(String sql) throws SQLException{
Statement sta = conn.createStatement();
return sta.execute(sql);
}
public boolean runSql2(String sql) throws SQLException {
Statement sta = conn.createStatement();
return sta.execute(sql);
}
@Override
protected void finalize() throws Throwable{
if (conn!=null || !conn.isClosed()){
conn.close();
}
}
}