Mysql jar包:
<!-- mysql 链接需要-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>
JDBC连接
package jdbc;
import java.sql.*;
/**
* Created with IntelliJ IDEA.
* User: Alvin
* Date: 14-8-3
* Time: 上午12:44
* To change this template use File | Settings | File Templates.
*/
public class Jdbc {
public static void main(String[] args) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ippost?characterEncoding=UTF-8", "username", "password");
String sql = "select * from ip";
Statement st = con.createStatement();
ResultSet resultSet = st.executeQuery(sql);
while (resultSet.next()) {
System.out.print(resultSet.getInt("id"));
System.out.print(" " + resultSet.getDate("addtime"));
System.out.println();
}
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}