|
本帖最后由 4378430 于 2018-1-16 00:29 编辑
- package com.wangluo.demo;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import com.wangluo.jdbcutils.JDBCUtils;
- import com.wwangluo.domain.Sort;
- public class JDBCDemo {
- public static void main(String[] args) throws SQLException {<div class="blockcode"><blockquote>package com.wangluo.jdbcutils;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class JDBCUtils {
- private JDBCUtils() {
- };
- private static Connection con;
- static {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost:3306/mybase";
- String ursename = "root";
- String password = "root";
- con = DriverManager.getConnection(url, ursename, password);
- } catch (Exception ex) {
- throw new RuntimeException(ex + "数据库连接失败");
- }
- }
- public static Connection getConnection() {
- return con;
- }
- public static void close(Connection con, Statement sta, ResultSet rs) {
- if (con != null) {
- try {
- con.close();
- } catch (SQLException ex) {
- }
- }
- if (sta != null) {
- try {
- sta.close();
- } catch (SQLException ex) {
- }
- }
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException ex) {
- }
- }
- }
- public static void close(Connection con, Statement sta) {
- if (con != null) {
- try {
- con.close();
- } catch (SQLException ex) {
- }
- }
- if (sta != null) {
- try {
- sta.close();
- } catch (SQLException ex) {
- }
- }
- }
- }
复制代码
- package com.wwangluo.domain;
- public class Sort {
- private int sid;
- private String sname;
- private double sprice;
- private String sdesc;
- public Sort() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Sort(int sid, String sname, double sprice, String sdesc) {
- super();
- this.sid = sid;
- this.sname = sname;
- this.sprice = sprice;
- this.sdesc = sdesc;
- }
- public int getSid() {
- return sid;
- }
- public void setSid(int sid) {
- this.sid = sid;
- }
- public String getSname() {
- return sname;
- }
- public void setSname(String sname) {
- this.sname = sname;
- }
- public double getSprice() {
- return sprice;
- }
- public void setSprice(double sprice) {
- this.sprice = sprice;
- }
- public String getSdesc() {
- return sdesc;
- }
- public void setSdesc(String sdesc) {
- this.sdesc = sdesc;
- }
- @Override
- public String toString() {
- return "Sort [sid=" + sid + ", sname=" + sname + ", sprice=" + sprice + ", sdesc=" + sdesc + "]";
- }
-
- }
复制代码
- package com.wangluo.demo;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import com.wangluo.jdbcutils.JDBCUtils;
- import com.wwangluo.domain.Sort;
- public class JDBCDemo {
- public static void main(String[] args) throws SQLException {
- // 调用JDBC工具类直接获取数据库连接
- Connection con = JDBCUtils.getConnection();
- // 连接获取sql语句对象
- PreparedStatement pst = con.prepareStatement("SELECT * FROM sort");
- // 调用cha询方法获取结果集
- ResultSet rs = pst.executeQuery();
- // 创建集合对象
- List<Sort> list = new ArrayList<Sort>();
- // 获取的每个对象封装到Sort对象中
- while (rs.next()) {
- Sort s = new Sort(rs.getInt("sid"), rs.getString("sname"), rs.getDouble("sprice"), rs.getString("sdesc"));
- list.add(s);
- }
- for (Sort s : list) {
- System.out.println(s);
- }
- JDBCUtils.close(con, pst, rs);
- }
- }
复制代码
DBUtils提供了一些工具类:就是为了让我们对sql的操作更加方便
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|