Android问题:使用jdbc向MySQL数据库插入记录

Android问题:使用jdbc向MySQL数据库插入记录,第1张

package com.example.MyClass

import java.sql.Connection

import java.sql.DriverManager

import java.sql.PreparedStatement

import java.sql.ResultSet

import java.sql.SQLException

//新建一个java类把公有的东西都抽出来,方便扩展节省代码。==20130120又练习的。

public class MyJdbc {

// 拿到连接的对象。

public Connection getConnection() {

// String driver =

// "com.mysql.jdbc.Driver"//因为现在不需要forClass映射驱动了,所以这个也可不要了。

String url = "jdbc:mysql://localhost:3306/mysql0120"// 数据库名mysql0120

String user = "root"// 数据库登陆用户

String password = "root2"// 数据库登陆密码

Connection con = null

try {

con = DriverManager.getConnection(url, user, password)// 那四句加这句就行了。

} catch (SQLException e) {

e.printStackTrace()

}

return con// 调这个方法就返回一个连接

}

// insert delete update===========-=-==增加,删除,更改都用这个通用方法=================

// 直接把sql语句和需要的参数(因为多个,所以用数组存)传过来。

public int update(String sql, Object[] parms) {

int row = 0

Connection con = this.getConnection()

PreparedStatement ps = null

try {

ps = con.prepareStatement(sql)

if (parms != null) {

for (int i = 0i <parms.lengthi++) {

ps.setObject(i + 1, parms[i])// 注意是i+1【从第一行算的】

}

}

row = ps.executeUpdate()// 返回影响的行数

} catch (SQLException e) {

e.printStackTrace()

} finally {

this.closeAll(con, ps, null)// 这不是查询【所以结果集是null】。

}

return row

}

// 所有关闭也是一样的,所以通用这个关闭方法。===-==========关闭==========

public void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {

try {

if (rs != null)

rs.close()

if (ps != null)

ps.close()

if (con != null)

con.close()

} catch (SQLException e) {

e.printStackTrace()

}

}

}

首先定义一个MySQL存储过程:

DROP PROCEDURE IF EXISTS test

CREATE PROCEDURE test (IN p1 VARCHAR(26), OUT pResult VARCHAR(512))

BEGIN

SET pResult := NULL

SET pResult :=CONCAT ( 'test',p1)

SELECT * FROM tb2 WHERE commet like Concat('%',p1, '%')-- this 返回一个结果集

END

tb2的表结构和数据如下:

DROP TABLE IF EXISTS `tb2`

CREATE TABLE `tb2` (

`t_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',

`Name` varchar(255) DEFAULT NULL COMMENT '字段名',

`type` int(1) DEFAULT NULL COMMENT '类型,0-正常,1-异常,2-传输,3-退单',

`commet` varchar(255) DEFAULT NULL COMMENT '备注',

`optime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT ' *** 作时间',

PRIMARY KEY (`t_id`)

)

继续增加新的类:

Stb2.Java内容

package com.springdemo.usermgr.vo

import java.util.Date

/**

* tb2表实体类

* @author zhouxj

* @date 2014-09-10 下午03:29:32

*/

public class Stb2{

private Integer t_id

private String name

private Integer type

private String commet

private java.util.Date optime

public Integer getT_id() {

return t_id

}

public void setT_id(Integer t_id) {

this.t_id = t_id

}

public String getName() {

return name

}

public void setName(String name) {

this.name = name

}

public Integer getType() {

return type

}

public void setType(Integer type) {

this.type = type

}

public String getCommet() {

return commet

}

public void setCommet(String commet) {

this.commet = commet

}

public java.util.Date getOptime() {

return optime

}

public void setOptime(java.util.Date optime) {

this.optime = optime

}

}

修改SUserMapper.java内容:

package com.springdemo.usermgr.vo

import java.util.List

import java.util.Map

public interface SUserMapper {

public int insertSUser(SUser user)

public SUser getSUser(String name)

public List<Stb2>getTestProc(Map<String, Object>param)

}

Test2测试类内容:

package domain

import java.io.IOException

import java.io.Reader

import java.util.HashMap

import java.util.List

import java.util.Map

import org.apache.ibatis.io.Resources

import org.apache.ibatis.session.SqlSession

import org.apache.ibatis.session.SqlSessionFactory

import org.apache.ibatis.session.SqlSessionFactoryBuilder

import com.springdemo.usermgr.vo.SUserMapper

import com.springdemo.usermgr.vo.Stb2

public class Test2 {

public static void main(String[] args) throws IOException {

String resource = "config.xml"

Reader reader = Resources.getResourceAsReader(resource)

SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader)

SqlSession session = ssf.openSession(true)//true 为自动提交事务

try {

Map<String, Object>parms = new HashMap<String, Object>()

parms.put("queryStr", "的")

SUserMapper spMapper = session.getMapper(SUserMapper.class)

List<Stb2>as=spMapper.getTestProc(parms)

String outPara=(String) parms.get("retStr")

System.out.println(outPara)

System.out.println(as.toString())

//org.apache.ibatis.type.JdbcType.VARCHAR

// org.apache.ibatis.mapping.ParameterMode.OUT

} catch (Exception e) {

e.printStackTrace()

} finally {

session.close()

}

}

}

config.xml配置文件修改内容:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration PUBLIC

"-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<typeAliases>

<typeAlias alias="SUser" type="com.springdemo.usermgr.vo.SUser" />

<typeAlias alias="Stb2" type="com.springdemo.usermgr.vo.Stb2" />

</typeAliases>

<environments default="development">

<environment id="development">

<transactionManager type="JDBC" />

<dataSource type="POOLED">

<property name="driver" value="com.mysql.jdbc.Driver" />

<property name="url" value="jdbc:mysql://localhost:3306/test"/>

<property name="username" value="root" />

<property name="password" value="pass" />

</dataSource>

</environment>

</environments>

<mappers>

<mapper resource="SUser.xml" />

<mapper resource="Stb2.xml" />

</mappers>

</configuration>

增加配置文件Stb2.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC

"-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.springdemo.usermgr.vo.SUserMapper">

<resultMap type="com.springdemo.usermgr.vo.Stb2" id="itemResult">

</resultMap>

<select id="getTestProc" parameterType="java.util.Map" statementType="CALLABLE"

resultMap="itemResult">

{call test.test(

#{queryStr,jdbcType=VARCHAR,mode=IN},

#{retStr,jdbcType=VARCHAR,mode=OUT})

}

</select>

</mapper>

运行Test2类,可能的结果:

test的

[com.springdemo.usermgr.vo.Stb2@6900bf61, com.springdemo.usermgr.vo.Stb2@3014af22]

返回了参数内容,和两条记录。


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存