A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 nuoxi0318 于 2013-7-31 07:55 编辑
  1. import java.sql.*;

  2. /**
  3. * 第一个 JDBC 的 HelloWorld 程序, 数据库访问MySQL.
  4. *
  5. *
  6. */
  7. public class test {
  8.         public static void main(String[] args) {
  9.                 // 1. 注册驱动
  10.                 try {
  11.                         Class.forName("com.mysql.jdbc.Driver");
  12.                 } catch (ClassNotFoundException e) {
  13.                         // TODO Auto-generated catch block
  14.                         e.printStackTrace();
  15.                 }// Mysql 的驱动
  16.                 // 先定义变量,后使用和关闭
  17.                 Connection conn = null;// 数据库连接
  18.                 Statement stmt = null;// 数据库表达式
  19.                 ResultSet rs = null;// 结果集
  20.                 try {
  21.                         conn = java.sql.DriverManager
  22.                                         .getConnection(
  23.                                                         "jdbc:mysql://localhost:3306:MySQL","root", "frank");// root是用户名,密码为空
  24.                         // 3. 获取表达式
  25.                         stmt = conn.createStatement();
  26.                         // 执行插入数据的 SQL
  27.                         int row = stmt
  28.                                         .executeUpdate("insert into Student(username,password, age) values('张三', '1234', 20)");
  29.                         row = stmt
  30.                         .executeUpdate("insert into Student(username,password, age) values('贝贝', '1111', 27)");
  31.                         System.out.println("插入了 " + row);
  32.                         // 4. 执行 SQL
  33.                         rs = stmt.executeQuery("select * from Student");
  34.                         // 5. 显示结果集里面的数据
  35.                         while (rs.next()) {
  36.                                 System.out.println("编号=" + rs.getInt(1));
  37.                                 System.out.println("学生姓名=" + rs.getString("username"));
  38.                                 System.out.println("密码=" + rs.getString("password"));
  39.                                 System.out.println("年龄=" + rs.getString("age"));
  40.                         }
  41.                         // 执行删除数据的 SQL, 被删除的记录的ID为7
  42.                         row = stmt.executeUpdate("delete from student where id = 2");
  43.                         System.out.println("删除了 " + row);
  44.                 } catch (SQLException e) {
  45.                         e.printStackTrace();
  46.                 } finally {
  47.                         // 6. 释放资源,建议放在finally语句中确保都被关闭掉了
  48.                         try {
  49.                                 rs.close();
  50.                         } catch (SQLException e) {
  51.                         }
  52.                         try {
  53.                                 stmt.close();
  54.                         } catch (SQLException e) {
  55.                         }
  56.                         try {
  57.                                 conn.close();
  58.                         } catch (SQLException e) {
  59.                         }
  60.                 }
  61.         }
  62. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杜光 + 1 每天提问并回答问题,是对知识的复习和积累.

查看全部评分

4 个回复

正序浏览
你的代码我没跑因为我没你的数据库,粗略看下可能是这几个小问题:
第一:我不知道你的数据库驱动jar包有没有导包

第二:
  1. int row = stmt

  2. 29.                                        .executeUpdate("insert into Student(username,password, age) values('张三', '1234', 20)");

  3. 30.                        row = stmt

  4. 31.                        .executeUpdate("insert into Student(username,password, age) values('贝贝', '1111', 27)");

复制代码
row没有赋初值,,,第二次怎么能引用的呢?

第三:

  1. <p>                      System.out.println("年龄=" + rs.getString("age"));</p>
复制代码
你在上面插入的年龄是int类型,,但是为什么你检索数据库字段值的时候是rs.getString(“age”)呢?怎么不是rs.getInt("age");

回复 使用道具 举报
空指针异常,最后复制一下控制台报错消息
回复 使用道具 举报
Class.forName("com.mysql.jdbc.Driver");  明显错了
回复 使用道具 举报
错误可能产生的原因:
1. Class.forName("com.mysql.jdbc.Driver");
    mysql的驱动jar包有没有加?
2.com.jdbc.mysql.Driver驱动类写错了吧?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马