代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
随机生成中文名称使用jdbc存储到数据库
作者:
骑毛驴的卡卡
/ 发布于
2014/11/7
/
654
随机生成中文名称,然后使用中文首字母+123作为用户密码,然后jdbc保存到数据库!
CREATE TABLE `user_info` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(32) DEFAULT NULL, `password` varchar(32) DEFAULT NULL, `status` int(11) DEFAULT NULL, `descn` varchar(64) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; package com.senvn.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.List; import com.senvn.bean.User; /** * * @author 骑毛驴的卡卡 * */ public class UserDao { private final static String DRIVER_CLASS = "com.mysql.jdbc.Driver"; private final static String DB_URL = "jdbc:mysql://localhost:3306/my_db?characterEncoding=UTF-8"; private final static String USER_NAME = "root"; private final static String PASSWORD = "root"; static { try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection(DB_URL, USER_NAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * 插入用户 * * @param u * @return */ public boolean insertUser(User u) { String sql = "INSERT INTO user_info(username,password,status,descn) VALUES(?,?,?,?)"; Connection conn = getConnection(); PreparedStatement statement = null; try { statement = conn.prepareStatement(sql); statement.setString(1, u.getUsername()); statement.setString(2, u.getPassword()); statement.setInt(3, u.getStatus()); statement.setString(4, u.getDescn()); int resultCode = statement.executeUpdate(); if (resultCode == 1) { System.out.println("用户插入成功!"); return true; } } catch (SQLException e) { e.printStackTrace(); } finally { try { statement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return false; } /** * 批量插入用户 * * @param users * @return */ public boolean batchInsertUser(List<User> users) { Connection conn = getConnection(); Statement statement = null; try { statement = conn.createStatement(); for (User u : users) { StringBuffer insertSql = new StringBuffer( "INSERT INTO user_info(username,password,status,descn) VALUES("); insertSql.append("'").append(u.getUsername()).append("'") .append(","); insertSql.append("'").append(u.getPassword()).append("'") .append(","); insertSql.append("'").append(u.getStatus()).append("'") .append(","); insertSql.append("'").append(u.getDescn()).append("'") .append(")"); statement.addBatch(insertSql.toString()); System.out.println(insertSql.toString()); } int[] resultCode = statement.executeBatch(); for (int i : resultCode) { System.out.println(i); } } catch (SQLException e) { e.printStackTrace(); } return false; } /** * 获取用户信息 * * @param id * @return */ public User getUserById(int id) { String sql = "SELECT u.id,u.username,u.`password`,u.`status`,u.descn FROM user_info AS u where u.id = ?"; Connection conn = getConnection(); PreparedStatement statement = null; ResultSet rs = null; User u = new User(); try { statement = conn.prepareStatement(sql); statement.setLong(1, id); rs = statement.executeQuery(); // 获取resultSet信息 // ResultSetMetaData rsmd=rs.getMetaData(); // System.out.println(rsmd.getColumnCount()); // System.out.println(rsmd.getColumnLabel(2)); while (rs.next()) { u.setId(rs.getInt(1)); u.setUsername(rs.getString(2)); u.setPassword(rs.getString(3)); u.setStatus(rs.getInt(4)); u.setDescn(rs.getString(5)); } } catch (SQLException e) { e.printStackTrace(); } finally { try { rs.close(); statement.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return u; } } 3. [文件] User.java ~ 1KB 下载(1) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 package com.senvn.bean; /** * * @author 骑毛驴的卡卡 * */ public class User { /** * 主键id */ private int id; /** * 用户名 */ private String username; /** * 密码 */ private String password; /** * 状态 */ private int status; /** * 描述 */ private String descn; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getDescn() { return descn; } public void setDescn(String descn) { this.descn = descn; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", status=" + status + ", descn=" + descn + "]"; } } 4. [文件] UserDaoTest.java ~ 3KB 下载(1) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 package com.senvn.test; import java.util.ArrayList; import java.util.List; import java.util.Random; import net.sourceforge.pinyin4j.PinyinHelper; import com.senvn.bean.User; import com.senvn.dao.UserDao; /** * * @author 骑毛驴的卡卡 * */ public class UserDaoTest { public static void main(String[] args) { // testAddUserList(4); // testAddUser("张三丰", "武当山掌门"); testGetUser(3); } /** * 添加用户 * * @param name * @param desc */ public static void testAddUser(String name, String desc) { User u = new User(); u.setPassword(generatorPwd(name)); u.setStatus(1); u.setDescn(desc); UserDao dao = new UserDao(); dao.insertUser(u); } /** * 测试添加批量用户 * * @param size */ public static void testAddUserList(int size) { List<User> users = new ArrayList<User>(); List<String> cnNames = randomCnName(size); for (int i = 0; i < size; i++) { User u = new User(); u.setUsername(cnNames.get(i)); u.setPassword(generatorPwd(cnNames.get(i))); u.setStatus(1); u.setDescn("测试批量产生的数据!"); users.add(u); } UserDao dao = new UserDao(); dao.batchInsertUser(users); } /** * 添加用户 * * @param name * @param desc */ public static void testGetUser(int id) { UserDao dao = new UserDao(); User u = dao.getUserById(id); System.out.println(u.toString()); } /** * 随机生成特定数目的中文名字 * * @param size * @return */ public static List<String> randomCnName(int size) { List<String> nameList = new ArrayList<String>(); String[] first = { "张", "王", "李", "赵", "钱", "孙", "高", "曹", "何", "刘" }; String[] second = { "文", "志", "守", "海", "永", "家", "锦", "士", "玉", "泽" }; String[] third = { "兵", "伟", "一", "洋", "星", "凯", "琼", "强", "刚", "雨", }; int[] randomLen = { 2, 3 }; Random random = new Random(); for (int i = 0; i < size; i++) { StringBuffer nameSbf = new StringBuffer(); int nameSize = randomLen[random.nextInt(2)]; if (nameSize > 2) { nameSbf.append(first[random.nextInt(10)]).append( third[random.nextInt(10)]); } else { nameSbf.append(first[random.nextInt(10)]) .append(second[random.nextInt(10)]) .append(third[random.nextInt(10)]); } nameList.add(nameSbf.toString()); } return nameList; } /** * 生成密码 * * @param cnName * @return */ public static String generatorPwd(String cnName) { StringBuffer pinyinHeaderChar = new StringBuffer(); for (int i = 0; i < cnName.length(); i++) { char zi = cnName.charAt(i); String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(zi); if (pinyin.length > 0) { pinyinHeaderChar.append(pinyin[0].charAt(0)); } else { pinyinHeaderChar.append("?"); } } pinyinHeaderChar.append(123); return pinyinHeaderChar.toString(); } }
试试其它关键字
数据库
中文名称
jdbc
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
骑毛驴的卡卡
贡献的其它代码
(
2
)
.
在线XML解析
.
随机生成中文名称使用jdbc存储到数据库
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3