当前位置: 代码迷 >> Java Web开发 >> 用java类生成sql语句的时候...问题来了.STREAM DATA
  详细解决方案

用java类生成sql语句的时候...问题来了.STREAM DATA

热度:645   发布时间:2012-08-06 10:33:48.0
用java类生成sql语句的时候...问题来了.STREAM DATA
问题应该出在SessionImpl类,希望早点有高人来解答!

这是User类对应数据库中t_user表

package entity;



import util.EntityAnno;

import util.ColumnAnno;



@EntityAnno(getTableName="t_user")

public class User {

private int id;

@ColumnAnno(getColumnName="username")

private String userName;

@ColumnAnno(getColumnName="name")

private String name;

@ColumnAnno(getColumnName="password")

private String password;

@ColumnAnno(getColumnName="gender")

private char sex;



public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public char getSex() {

return sex;

}

public void setSex(char sex) {

this.sex = sex;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

}

这是SessionImpl类,问题应该就在这个里面
package util.session.impl;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.sun.corba.se.spi.orbutil.fsm.FSM;
import com.sun.xml.internal.stream.Entity;

import entity.User;

import util.ColumnAnno;
import util.EntityAnno;
import util.JdbcUtil;
import util.session.Session;

/**
* 获得增删改查的sql语句 如果类变量没有注释,则认为该变量对应的数据库表的列是自动生成的,不可更改
* EntityAnno和ColumnAnno是两个注释类分别获取表名和列名
* 如果类没有注释默认表名和类名相同
* 如果类里的变量没有注释默认表中该列名是自动生成,不需要更改
*
* @author King
*
*/
public class SessionImpl implements Session {

/**
  * 获取添加的sql语句
  *
  * @param obj
  * @return
  */
private String getAddSql(Object obj) {
StringBuilder sb = new StringBuilder();
sb.append("insert into ");
Class c = obj.getClass();
String simpleClassName = c.getSimpleName();
// 如果没有注解,认为表名和类名一样
String tableName = simpleClassName;
EntityAnno ea = (EntityAnno) c.getAnnotation(EntityAnno.class);
if (ea != null) {
tableName = ea.getTableName();
}
sb.append(tableName).append("(");
// 获取所有属性
Field[] fs = c.getDeclaredFields();
int flag = 0;
for (int i = 0; i < fs.length; i++) {
String columnName = null;
ColumnAnno ca = (ColumnAnno) fs[i].getAnnotation(ColumnAnno.class);

if (ca != null) {
flag++;
columnName = ca.getColumnName();
sb = i == fs.length - 1 ? sb.append(columnName) : sb.append(
columnName).append(",");
}
}
sb.append(") values ").append(getString(flag));
return sb.toString();
}

/**
  * 获得类似(?,?,?)这样的语句
  *
  * @param length
  * @return
  */
private static String getString(int length) {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (int i = 0; i < length; i++) {
sb = i == 0 ? sb.append("?") : sb.append(",?");
}
sb.append(")");
return sb.toString();
}

/**
  * 获得删除的sql语句
  *
  * @param obj
  * @return 类似delete from t_user where id=3;
  */
private String getDeleteSql(Object obj, String str) {
StringBuilder sb = new StringBuilder();
sb.append("delete from ");
Class c = obj.getClass();
String tableName = c.getSimpleName();
EntityAnno ea = (EntityAnno) c.getAnnotation(EntityAnno.class);
if (ea != null) {
tableName = ea.getTableName();
}
sb.append(tableName).append(" where ");
int flag = getNumber(obj, str);
Field[] fs = c.getDeclaredFields();
ColumnAnno ca = (ColumnAnno) fs[flag].getAnnotation(ColumnAnno.class);
if (ca != null) {
sb.append(ca.getColumnName()).append("=?");
} else {
sb.append(fs[flag].getName()).append("=?");
}

return sb.toString();
}

/**
  * 获得查询的sql语句
  *
  * @param obj
  * @return 类似select * from t_user;
  */
private String getQuerySql(Object obj) {
StringBuilder sb = new StringBuilder();
sb.append("select * from ");
Class c = obj.getClass();
String tableName = c.getSimpleName();
EntityAnno ea = (EntityAnno) c.getAnnotation(EntityAnno.class);
if (ea != null) {
tableName = ea.getTableName();
}
sb.append(tableName);

return sb.toString();
}

/**
  * 获得更新的sql语句
  *
  * @param obj
  * @return 类似update t_user set name='b',password='d' where id=3;
  */
private String getUpdateSql(Object obj, String str) {
StringBuilder sb = new StringBuilder();
sb.append("update ");
Class c = obj.getClass();
String tableName = c.getSimpleName();
EntityAnno ea = (EntityAnno) c.getAnnotation(EntityAnno.class);
if (ea != null) {
tableName = ea.getTableName();
}
sb.append(tableName).append(" set ");
Field[] fs = c.getDeclaredFields();
String columnName = null;
int flag = getNumber(obj, str);
for (int i = 0; i < fs.length; i++) {
if (i != flag) {
ColumnAnno ca = fs[i].getAnnotation(ColumnAnno.class);
if (ca != null) {
columnName = ca.getColumnName();
} else {
continue;
}
} else {
continue;
}
sb = i == fs.length - 1 ? sb.append(columnName).append("=?") : sb
.append(columnName).append("=?,");
}
columnName = fs[flag].getName();
ColumnAnno ca = (ColumnAnno) fs[flag].getAnnotation(ColumnAnno.class);
if (ca != null) {
columnName = ca.getColumnName();
}
sb.append(" where ").append(columnName).append(" =?");

return sb.toString();
}

/**
  * 获得精确查询语句
  *
  * @param obj
  *            ,o
  * @return 类似select * from t_user where id=3;
  */
private String getQuerySql(Object obj, String str) {
StringBuilder sb = new StringBuilder();
sb.append("select * from ");
Class c = obj.getClass();
String tableName = c.getSimpleName();
EntityAnno ea = (EntityAnno) c.getAnnotation(EntityAnno.class);
if (ea != null) {
tableName = ea.getTableName();
}
sb.append(tableName).append(" where ");
int flag = getNumber(obj, str);
Field[] fs = c.getDeclaredFields();
String columnName = fs[flag].getName();
ColumnAnno ca = (ColumnAnno) c.getAnnotation(ColumnAnno.class);
if (ca != null) {
columnName = ca.getColumnName();
}
sb.append(columnName).append("=?");

return sb.toString();
}

/**
  * 获取sql语句where后面的元素是类中第几个字段
  *
  * @param obj
  *            , o
  * @return
  */
private int getNumber(Object obj, String str) {
int i = 0;
Class c = obj.getClass();
Field[] fs = c.getDeclaredFields();
for (i = 0; i < fs.length; i++) {
if (fs[i].getName() == str) {
break;
}
}
return i;
}

public PreparedStatement getAddPstmt(Connection con, Object obj) {
PreparedStatement pstmt = null;
StringBuilder sb = new StringBuilder(getAddSql(obj));
try {
pstmt = con.prepareStatement(getAddSql(obj));
Class c = obj.getClass();
Field[] fs = c.getDeclaredFields();
int flag = 0;

for (int i = 0; sb.toString().endsWith("?)") && i < fs.length; i++) {
// System.out.println(i);
ColumnAnno ca = (ColumnAnno) fs[i]
.getAnnotation(ColumnAnno.class);
if (ca != null) {
fs[i].setAccessible(true);
flag++;
try {
pstmt.setObject(flag, fs[i].get(obj));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
// for (int i = 0; i < fs.length; i++) {
// ColumnAnno ca = fs[i].getAnnotation(ColumnAnno.class);
// if (ca != null) {
// flag++;
// fs[flag].setAccessible(true);
// try {
// pstmt.setObject(flag, fs[flag].get(obj));
// System.out.println(pstmt.toString());
// } catch (IllegalArgumentException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// }
// }
// }

} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}

public PreparedStatement getDeletePstmt(Connection con, Object obj,
String str) {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(getDeleteSql(obj, str));
Class c = obj.getClass();
Field[] fs = c.getDeclaredFields();
int i = getNumber(obj, str);
fs[i].setAccessible(true);
try {
pstmt.setObject(1, fs[i].get(obj));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}

public PreparedStatement getQueryPstmt(Connection con, Object obj,
String str) {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(getQuerySql(obj, str));
Class c = obj.getClass();
Field[] fs = c.getDeclaredFields();
int i = getNumber(obj, str);
fs[i].setAccessible(true);
try {
pstmt.setObject(1, fs[i].get(obj));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}

public PreparedStatement getQueryPstmt(Connection con, Object obj) {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(getQuerySql(obj));
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}

public PreparedStatement getUpdatePstmt(Connection con, Object obj,
String str) {
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(getUpdateSql(obj, str));
Class c = obj.getClass();
Field[] fs = c.getDeclaredFields();
int flag = 1;
for (int i = 0; i < fs.length; i++) {
if (i != getNumber(obj, str)) {
ColumnAnno ca = fs[i].getAnnotation(ColumnAnno.class);
if (ca != null) {
fs[i].setAccessible(true);
try {
pstmt.setObject(flag, fs[i].get(obj));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
continue;
}
} else {
continue;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}

public static void main(String[] args) {
User user = new User();
user.setId(10);
user.setName("qqq");
user.setUserName("aaa");
user.setPassword("xxxx");
user.setSex('m');
Session s = new SessionImpl();
Connection con = JdbcUtil.getConnection();
System.out.println(s.getAddPstmt(con, user).toString());
System.out.println(s.getDeletePstmt(con, user, "id").toString());
System.out.println(s.getUpdatePstmt(con, user, "name").toString());
System.out.println(s.getQueryPstmt(con, user).toString());
System.out.println(s.getQueryPstmt(con, user, "password"));

}

}
运行结果是:
连接成功!
com.mysql.jdbc.JDBC4PreparedStatement@12a3793: insert into t_user(username,name,password,gender) values ('aaa','qqq','xxxx',** STREAM DATA **)
com.mysql.jdbc.JDBC4PreparedStatement@8acf6e: delete from t_user where id=10
com.mysql.jdbc.JDBC4PreparedStatement@1362012: update t_user set username=** STREAM DATA **,password=** NOT SPECIFIED **,gender=** NOT SPECIFIED ** where name =** NOT SPECIFIED **
com.mysql.jdbc.JDBC4PreparedStatement@1e59128: select * from t_user
com.mysql.jdbc.JDBC4PreparedStatement@9ced8e: select * from t_user where password='xxxx'
我不知道哪个STREAM DATA是什么意思,不知道怎么改正

这是Session接口
package util.session;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
* 获取增删改查sql语句
* @author King
*
*/
public interface Session {

/**
  * 获取添加的sql语句
  * @param con
  * @param obj 类字段的字符串,sql语句where后面的元素
  * @return
  */
PreparedStatement getAddPstmt(Connection con, Object obj);

/**
  * 获取删除的sql语句
  * @param con
  * @param obj
  * @param str 类字段的字符串,sql语句where后面的元素
  * @return
  */
PreparedStatement getDeletePstmt(Connection con, Object obj, String str);

/**
  * 获取更新的sql语句
  * @param con
  * @param obj
  * @param str 类字段的字符串,sql语句where后面的元素
  * @return
  */
PreparedStatement getUpdatePstmt(Connection con, Object obj, String str);

/**
  *获取精确查询的sql语句
  * @param con
  * @param obj
  * @param str 类字段的字符串,sql语句where后面的元素
  * @return
  */
PreparedStatement getQueryPstmt(Connection con, Object obj, String str);

/**
  * 获取查询的sql语句
  * @param con
  * @param obj
  * @return
  */
PreparedStatement getQueryPstmt(Connection con, Object obj);

}
搜索更多相关主题的帖子: 数据库  java  password  sql语句  private  

----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
  相关解决方案