Skip to content
Create a gist now

Instantly share code, notes, and snippets.

To Generate the count of special values with category from one field in one table of mysql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class FashionGenerate {
private static String url = "jdbc:mysql://localhost:3306/Database";
private static String user = "UserName";
private static String password = "Password";
private static Connection conn;
private static Connection update_conn;
private static Connection insert_conn;
private static Connection select_conn;
private static PreparedStatement ps;
private static PreparedStatement update_ps;
private static PreparedStatement insert_ps;
private static PreparedStatement select_ps;
private static ResultSet rs;
private static ResultSet select_rs;
private static String sql;
private static String update_sql;
private static String insert_sql;
private static String select_sql;
static {
try {
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
} catch (SQLException e) {
} finally {
try {
if (st != null)
} catch (SQLException e) {
} finally {
if (conn != null)
try {
} catch (SQLException e) {
private static void insert(String Prop, String Kind) throws SQLException {
insert_conn = getConnection();
insert_sql = "insert into prop_count set Prop = ? , Kind = ? , Num = 1";
insert_ps = conn.prepareStatement(insert_sql);
insert_ps.setString(1, Prop);
insert_ps.setString(2, Kind);
free(null, insert_ps, insert_conn);
private static void update(String Prop) throws SQLException {
update_conn = getConnection();
update_sql = "update prop_count set num = num+1 where prop = ?";
update_ps = update_conn.prepareStatement(update_sql);
update_ps.setString(1, Prop);
free(null, update_ps, update_conn);
private static void generate() throws SQLException {
conn = getConnection();
sql = "select fashion from item_detail;";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while( {
String fashion = rs.getString("fashion");
if((fashion==null) || fashion.equals(""))
fashion = fashion.trim();
select_conn = getConnection();
select_sql = "select prop from prop_count where prop = ? and kind = ?;";
select_ps = conn.prepareStatement(select_sql);
select_ps.setString(1, fashion);
select_ps.setString(2, "fashion");
select_rs = select_ps.executeQuery();
if( {
System.err.println("try updating");
else {
System.err.println("try inserting");
insert(fashion, "fashion");
free(select_rs, select_ps, select_conn);
free(rs, ps, conn);
public static void main(String[] args) throws SQLException {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.