JDBC 分页显示与自增长主键

一、表

create table client
(
  id int(3) primary key auto_increment,
  name varchar(20),
  password varchar(20)
);

二、JDBC工具类

package com.jdbc.demo;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * JDBC工具类,用来获得连接,关闭相关资源
 * @author jiazhengfeng
 *
 */
public class JDBCUtils {
	private static Connection con = null;
	private static String driver = null;
	private static String url = null;
	private static String username = null;
	private static String password = null;

	static {

		InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");

		try {

			Properties pro = new Properties();
			pro.load(in);
			driver = pro.getProperty("driver");
			url = pro.getProperty("url");
			username = pro.getProperty("username");
			password = pro.getProperty("password");

			Class.forName(driver);
			con = DriverManager.getConnection(url, username, password);

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		return con;
	}

	public static void release(Connection con, Statement st, ResultSet rs) {

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
三、数据库操作类


package com.jdbc.demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ListClient {
	private static Connection con = null;
	private static PreparedStatement st = null;
	private static ResultSet rs = null;

	public static void main(String[] args) {
		
		//getGeneratedKeys();
		int start=0;
		int count=3;
		list(start,count);
		
		
}
	/**
	 * 分页显示
	 * @param start
	 * @param count
	 */
	public static void list(int start, int count) {
		
		try {
			con = JDBCUtils.getConnection();
			Statement st = con.createStatement();
			String sql = "select *from client limit "+start+","+count;
			ResultSet rs = st.executeQuery(sql);
			
			while(rs.next()){
				int id=rs.getInt("id");
				String name=rs.getString("name");
				String password=rs.getString("password");
				System.out.printf("%d\t%s\t%s\t\n",id,name,password);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtils.release(con, st, rs);
		}
	}
	/**
	 * 得到自增长主键
	 */
	public static void getGeneratedKeys() {
		con = JDBCUtils.getConnection();
		try {
			con = JDBCUtils.getConnection();
			String sql = "insert into client(name,password) values(?,?);";
			PreparedStatement st = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
			st.setString(1, "xiaofang");
			st.setString(2, "123456");

			st.execute();
			ResultSet rs = st.getGeneratedKeys();
			while (rs.next()) {
				System.out.println(rs.getInt(1));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtils.release(con, st, rs);
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值