1.在新建的Project中右键新建Floder 2 2.创建名为lib的包 3.创建完毕之后的工程目录 4.接下来解压你下载的mysql的jar包,拷贝其中的.jar文件 5.在工程lib包下邮件 选择paste即粘贴,把mysql的jar包拷贝进来 6.拷贝完毕如图:7.在mysql的jar包上右键选择 build path - add to build path 8.添加...
首先要下载Connector/J地址:
http://www.mysql.com/downloads/connector/j/解压后得到jar库文件,需要在工程中导入该库文件,放在libs文件夹里
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBHelper {
public static final String url = "jdbc:mysql://127.0.0.1/student";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "root";
public Connection conn = null;
public PreparedStatement pst = null;
public DBHelper(String sql) {
try {
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn.prepareStatement(sql);//准备执行语句
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
this.conn.close();
this.pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}2017-08-25