如何用eclipse连接mysql数据库

如题所述

  方法/步骤

    在新建的Project中右键新建Floder,创建名为lib的包

    解压mysql的jar包,拷贝其中的.jar文件,在工程lib包下,把mysql的jar包拷贝进来

    在mysql的jar包上右键选择 build path - add to build path

    在工程中新建类,输入如下代码,就可以通过增删改查来操作数据库了:

  

    

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-04-29
/** * jdbc对mysql的retrieve操作 * 记得添加jar进来 * @param id * @throws Exception */publicstaticvoid retrieve(int id) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); //注意★ String sql = "select id,username,address from t_user where id=?"; PreparedStatement sta = con.prepareStatement(sql); sta.setInt(1, id); ResultSet rs = sta.executeQuery(); //注意:当现实一条记录时:while可以换成if。★if(rs.next()) { int did = rs.getInt("id"); String username = rs.get本回答被提问者采纳