java 如何利用list调用MySQL的数据

java 如何利用list调用MySQL的数据,第1张

// 1. 注册驱动

try {

Class.forName("com.mysql.jdbc.Driver")

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}// Mysql 的驱动

// 2. 获取数据库的连接

java.sql.Connection conn = java.sql.DriverManager.getConnection(

"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=GBK", "root", null)

// 3. 获取表达式

java.sql.Statement stmt = conn.createStatement()

// 执行插入数据的 SQL

stmt.executeUpdate("insert into MyUser(username, password) values('JDBC 中文测试', '密码')")

// 4. 执行 SQL

java.sql.ResultSet rs = stmt.executeQuery("select * from MyUser")

// 5. 显示结果集里面的数据

while(rs.next()) {

System.out.println(rs.getInt(1))

System.out.println(rs.getString("username"))

System.out.println(rs.getString("password"))

System.out.println()

}

// 6. 释放资源

rs.close()

stmt.close()

conn.close()

请参考//4部分

Statement换成preparedStatement,就有相应的set方法了。

或者既然addBatch里面传入的是String类型,那我们自己构造,

stmt.addBatch(“insert into users values ("+"values1"+","+"values2"+")")

或者既然是users类,我们可以根据users中属性是否为初始值来自动生成inset语句,下面是我以前写的代码,仅供参考

注释:

1、下面的User.NAME等就是user中name属性在表中的列名

2、方法ConvertStr就是把插入的列的value加上单引号。

  private static String ConvertStr(Object object) {

      return "'" + object.toString() + "'"

  }

3、其他

  public static final String strIns = "insert into users("

  public static final String strVal = ") values("

  public static final String strEnd = ")"

4、调用

stmt.addBatch(User.InsetStr(user))//这样就不用考虑传参了

代码如下:

public static String InsetStr(User user) {

String StrCol = ""

String Values = ""

if (user.getName() != null &&!user.getName().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.NAME

Values = Values + "," + ConvertStr(user.getName())

} else {

StrCol = StrCol + User.NAME

Values = Values + ConvertStr(user.getName())

}

}

if (user.getNick() != null &&!user.getNick().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.NICK

Values = Values + "," + ConvertStr(user.getNick())

} else {

StrCol = StrCol + User.NICK

Values = Values + ConvertStr(user.getNick())

}

}

if (user.getStudentid() != 0) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.STUDENTID

Values = Values + "," + ConvertStr(user.getStudentid())

} else {

StrCol = StrCol + User.STUDENTID

Values = Values + ConvertStr(user.getStudentid())

}

}

if (user.getSex() != '\u0000') {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.SEX

Values = Values + "," + ConvertStr(user.getSex())

} else {

StrCol = StrCol + User.SEX

Values = Values + ConvertStr(user.getSex())

}

}

if (user.getPassword() != null &&!user.getPassword().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.PASSWORD

Values = Values + "," + ConvertStr(user.getPassword())

} else {

StrCol = StrCol + User.PASSWORD

Values = Values + ConvertStr(user.getPassword())

}

}

if (user.getHash() != null &&!user.getHash().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.HASH

Values = Values + "," + ConvertStr(user.getHash())

} else {

StrCol = StrCol + User.HASH

Values = Values + ConvertStr(user.getHash())

}

}

if (user.getSchool() != null &&!user.getSchool().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.SCHOOL

Values = Values + "," + ConvertStr(user.getSchool())

} else {

StrCol = StrCol + User.SCHOOL

Values = Values + ConvertStr(user.getSchool())

}

}

if (user.getMajor() != null &&!user.getMajor().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.MAJOR

Values = Values + "," + ConvertStr(user.getMajor())

} else {

StrCol = StrCol + User.MAJOR

Values = Values + ConvertStr(user.getMajor())

}

}

if (user.getMobile() != 0) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.MOBILE

Values = Values + "," + ConvertStr(user.getMobile())

} else {

StrCol = StrCol + User.MOBILE

Values = Values + ConvertStr(user.getMobile())

}

}

if (user.getCollege() != null &&!user.getCollege().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.COLLEGE

Values = Values + "," + ConvertStr(user.getCollege())

} else {

StrCol = StrCol + User.COLLEGE

Values = Values + ConvertStr(user.getCollege())

}

}

if (user.getGrade() != 0) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.GRADE

Values = Values + "," + ConvertStr(user.getGrade())

} else {

StrCol = StrCol + User.GRADE

Values = Values + ConvertStr(user.getGrade())

}

}

if (user.getBclass() != null &&!user.getBclass().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.BCLASS

Values = Values + "," + ConvertStr(user.getBclass())

} else {

StrCol = StrCol + User.BCLASS

Values = Values + ConvertStr(user.getBclass())

}

}

if (user.getIdnum() != null &&!user.getIdnum().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.IDNUM

Values = Values + "," + ConvertStr(user.getIdnum())

} else {

StrCol = StrCol + User.IDNUM

Values = Values + ConvertStr(user.getIdnum())

}

}

if (user.getEmail() != null &&!user.getEmail().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.EMAIL

Values = Values + "," + ConvertStr(user.getEmail())

} else {

StrCol = StrCol + User.EMAIL

Values = Values + ConvertStr(user.getEmail())

}

}

if (user.getRegip() != null &&!user.getRegip().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.REGIP

Values = Values + "," + ConvertStr(user.getRegip())

} else {

StrCol = StrCol + User.REGIP

Values = Values + ConvertStr(user.getRegip())

}

}

if (user.getRegdate() != 0) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.REGDATE

Values = Values + "," + ConvertStr(user.getRegdate())

} else {

StrCol = StrCol + User.REGDATE

Values = Values + ConvertStr(user.getRegdate())

}

}

if (user.getUnit() != null &&!user.getUnit().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.UNIT

Values = Values + "," + ConvertStr(user.getUnit())

} else {

StrCol = StrCol + User.UNIT

Values = Values + ConvertStr(user.getUnit())

}

}

if (user.getRegion() != null &&!user.getRegion().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.REGION

Values = Values + "," + ConvertStr(user.getRegion())

} else {

StrCol = StrCol + User.REGION

Values = Values + ConvertStr(user.getRegion())

}

}

if (user.getDepartments() != null &&!user.getDepartments().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.DEPARTMENTS

Values = Values + "," + ConvertStr(user.getDepartments())

} else {

StrCol = StrCol + User.DEPARTMENTS

Values = Values + ConvertStr(user.getDepartments())

}

}

if (user.getTdcj() >0) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.TDCJ

Values = Values + "," + ConvertStr(user.getTdcj())

} else {

StrCol = StrCol + User.TDCJ

Values = Values + ConvertStr(user.getTdcj())

}

}

if (user.getTzcj() >0) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.TZCJ

Values = Values + "," + ConvertStr(user.getTzcj())

} else {

StrCol = StrCol + User.TZCJ

Values = Values + ConvertStr(user.getTzcj())

}

}

if (user.getAddress() != null &&!user.getAddress().equals("")) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.ADDRESS

Values = Values + "," + ConvertStr(user.getAddress())

} else {

StrCol = StrCol + User.ADDRESS

Values = Values + ConvertStr(user.getAddress())

}

}

if (user.getPostcode() != 0) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.POSTCODE

Values = Values + "," + ConvertStr(user.getPostcode())

} else {

StrCol = StrCol + User.POSTCODE

Values = Values + ConvertStr(user.getPostcode())

}

}

if (user.getBankcard() != 0) {

if (!StrCol.equals("")) {

StrCol = StrCol + "," + User.BANKCARD

Values = Values + "," + ConvertStr(user.getBankcard())

} else {

StrCol = StrCol + User.BANKCARD

Values = Values + ConvertStr(user.getBankcard())

}

}

return Sql.strIns + StrCol + Sql.strVal + Values + Sql.strEnd

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7286239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存