JDBC API入门

2025-10-29 02:24:24

1、导包

在eclipse工程名下穿件lib文件夹并把jar包放入,右键buildpath即可

2、加载驱动:Class.forName("com.mysql.jdbc.Driver");

3,获得连接:Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");

括号中三个分别为:url,数据库用户名,密码;

3、获得SQL语句对象:

Statement statement = conn.createStatement();

5,执行SQL语句:

ResultSet rs = statement.executeQuery("select * from exam");

4、遍历结果: while (rs.next()) {

System.out.print(rs.getInt("id")+" ");

System.out.print(rs.getString("name")+" ");

System.out.print(rs.getInt("english")+" ");

System.out.print(rs.getInt("chinese")+" ");

System.out.print(rs.getInt("math")+" ");

System.out.println();

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:42
  • 阅读量:73
  • 阅读量:163
  • 阅读量:120
  • 阅读量:116
  • 猜你喜欢