日期:2014-05-16 浏览次数:20654 次
准备工作:安装XAMPP,登陆apache,mysql,并通过phpadmin来创建数据库,新建一个表,插入一些数据:
http://localhost/phpmyadmin,最好设置密码,不然后面连接数据库的时候可能会无法访问。
设置密码方式:
修改密码--->一定要使用生成的密码来登陆,包括后面的数据库url也是。
我自己的数据如下:包括8个字段,其中主键ID要勾选 auto increament,其余的数据你自己随意设置。
这时候就可以在eclipse中编程开发了,注意的是我们需要导入mysql数据库的驱动,就是一个jar文件:
代码如下:
package jdbc01; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class javajdbctest { static Connection conn; static Statement st; private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://127.0.0.1:3306/test_01_xampp"; private static String usr = "root"; //password for mysql private static String psw = "Ctx3azAvHKDVmSrM"; public static void main(String[] args) throws SQLException { insert(); update(); delete(); query(); } public static void insert() throws SQLException { conn = getcon(); String sql = "INSERT INTO staff(name, age, sex, address, depart, worklen, wage)" + " VALUES ('Tom1', 32, 'male', 'beijingchina', 'personnel', '3', '3000')"; st = conn.createStatement(); //执行插入操作的sql语句,并返回插入数据的个数 int cnt = st.executeUpdate(sql); //输出插入操作的处理结果 System.out.println("向staff表中插入 " + cnt + " 条数据"); conn.close(); //关闭数据库连接 } public static void update() throws SQLException { conn = getcon(); String sql = "update staff set wage = '2200' where name = 'Tom1'"; try { st = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } int cnt = st.executeUpdate(sql); System.out.println("staff表 更新: " + cnt + "条数据"); conn.close(); } public static void delete() throws SQLException { conn = getcon(); String sql = "delete from staff where name = 'Tom1'"; st = conn.createStatement(); int cnt = st.executeUpdate(sql); System.out.println("staff表中删除 " + cnt + " 条数据"); conn.close(); } public static void query() throws SQLException { conn = getcon(); String sql = "select * from staff"; st = conn.createStatement(); ResultSet rt = st.executeQuery(sql); System.out.println("query results belows: "); while(rt.next()) { String name = rt.getString("name"); String sex = rt.getString("sex"); int age = rt.getInt("age"); String address = rt.getString("address"); String depart = rt.getString("depart"); String worklen = rt.getString("worklen"); String wage = rt.getString("wage"); System.out.println(name + " " + age + " " + sex + " " + address + " " + depart + " " + worklen + " " + wage); } conn.close(); } public static Connection getcon() { try { Class.forName(driver); } catch (ClassNotFoundException e1) { System.out.println("加载驱动失败."); e1.printStackTrace(); } System.out.println("MySQL JDBC Driver R