学习jdbc(一周总结)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MrwanZH/article/details/76795235

声明:该总结为个人总结,所以记录得比较乱,看者勿喷

2017.8.3

1.dbutils(结合dbcp连接池版(单例  BasicDataSource bds;))

       内容------------

public static boolean execUpdate(Connection conn,String sql,Object ...objs)
public static <T> List<T> queryList(String sql,CallBack<T> call,Object...params)

list.add(objs)
public static <T> T queryOne(String sql,CallBack<T> call,Object...params)


2.java.util.Date转换成java.sql.Date  timestamp和数据库的datetime可以兼容

(1).ps.setDate(2, new java.sql.Date(newDate().getTime()))  根据long来转换 但是没有时分

扫描二维码关注公众号,回复: 4255199 查看本文章

(2).ps.setTimestamp(2, new java.sql.Timestamp(newDate().getTime())) 

         ps.setTimestamp(2, new Timestamp(System.currentTimeMillis()));

java.util.Date有三个子类:java.sql.Date  java.sql.Time java.sql.Timestamp

sql中:time只有时分秒 date只有年月日 timestamp都有

3.ps.get()的参数

Select count(*) as xx from employee

Ps.getInt(1)

Ps.getInt(“count(*)”)

Ps.getInt(“xx”)

4.ps=conn.prepareStatement("insertinto daily(content, sid) values(?, ?)",

Statement.RETURN_GENERATED_KEYS);

  ResultSet rs = ps. getGeneratedKeys()获得插入数据的主键

此处主键必须自动增长,除非插入的时候输入主键值,否则表中主键必须自动增长

5.获取结果元数据

       ResultSetMetaDatarsmd = rs.getMetaData();

rsmd.getColumnCount()获得列数

rsmd.getColunmLabel(index)获得别名

rsmd.getColumnName()获得名称

rsmd.getColumnTypes()获得一个int值代表Types 该列的类型

注意事项:num as number 列改了名字后,再用原列名rs.getObject(“num”)找不到该数据

6.jdbc调用存储过程

CallableStatement cs = getConn().prepareCall(“{callsp_paging(?,?,?,?,?,?,?,?,?)}”);

//输入

cs.setInt(1,int);

cs.setString(2,String);

  //注册输出参数

  cs.registerOutParameter(8,java.sql.Types.INTEGER)

  //执行存储过程

  cs.execute();   不用其他的 因为可能有更新也有查询

7.Object ...obj表示不定长度的数组  不传参数则obj长度为0

8.DTO和DAO

 

2017.8.4

1.连接池(避免频繁通过数据库获取连接(资源开销太大))(dbcp、c3p0更稳定)

二者的区别dbcp没有自动的去回收空闲连接的功能(超过响应时间或最大连接数会断掉所有连接)

c3p0有自动回收空闲连接功能

简单自定义连接池的实现:(懒汉模式  DataSource)

(1)static LinkedList<Connection>pools = new LinkedList<>();// 存储数据库连接的容器


(2)初始化连接池(add 往集合里添加n个conn)conn =DriverManager.getConnection(url, username, password);

(3)使用者调用getConn获得连接,先判断空闲连接数,若不够则再add然后在分配给用户

(4)释放连接,重新add到pools数组中,修改空闲连接和激活连接的值

2. executeBatch();的使用方法

3.事务(更新操作) 要么同时成功要么同时失败

例子:

只有同时成功的时候才能提交,否则回滚


2017.8.5

1.数据库存储文件(字符流文本、字节流文件)

       PreparedStatementps = conn.prepareStatement("insert into tb_clob(fname,content)values(?,?)");

ps. setCharacterStream(2, FileReader)       //传入字符流文本

ps. setBinaryStream(2, FileInputStream)     //传入字节流文件

2.获取文件

       先用二进制流获取二进制文件

       InputStream is = rs.getBinaryStream("file");

然后用bufferedInputStream包装is  用BufferedOutputStream写出来

B = Byte[1024]

While(int len =bis.read(b) != -1)

       Bos.write(b,0,len)

3.Packetfor query is too large(mysql写入数据过大)

       My.ini加一行 max_allowed_packet = 4M  重启MySql服务

4.conn.getMetaData().getURL()获取ip和端口号 

5. 如果向mysql中存储二进制文件时,数据库的字符集不能设置为gbk,推荐使用UTF-8(设置向导 改)

6. c3p0连接池的工具类DataSource(连接池为单例,连接不可单例)

       privatestatic ComboPooledDataSource dataSource = new ComboPooledDataSource();

(饿汉模式)

  加载系统属性步骤           //属性文件 get 和new的区别??

Properties props= new Properties();  Properties props = System.getProperties();

Props.load (DBConnection.class.getResourceAsStream("文件路径,可以放在同级包内"));

       url= props.getProperty("url");通过key值获取Meaning 

      

 

各数据库的驱动

####mysql connectioninfo####

driver = com.mysql.jdbc.Driver

url = jdbc:mysql://127.0.0.1:3306/mydb

user = root

password = zenghui.111

 

####MSSQLServer connectioninfo####

#driver =com.microsoft.sqlserver.jdbc.SQLServerDriver

#url =jdbc:sqlserver://127.0.0.1:1433;databaseName=test

#user = sa

#password = zenghui.111

 

 

####Oracle connectioninfo####

#driver =com.oracle.driver.OracleDriver

#url =jdbc:oracle:thin:@127.0.0.1:1521:orcl

#user = scott

#password = zenghui.111


7.homework  

  ps.setObject(i+1,objs[i]);用setObject可以将util.date类的对象存到数据库的sql.date类

  like ?   ----   "%"+goodsname+“%”   

  string.contains(String)// 包含某字符串

字符串转换成各类型:

static SimpleDateFormat sdf = newSimpleDateFormat("MM/dd/yyyy"); date=sdf.parse(String)

s=sdf.format(date)--------date转换成字符串

int--Pattern.matches("^\\d*$",s)---Integer.parseInt(s)

double--Pattern.matches("^\\d+\\.??\\d+$",s)--- Double.parseDouble(s)

BigDecimal---Pattern.matches("^\\d+\\.??\\d+$",s)---new BigDecimal(s)

猜你喜欢

转载自blog.csdn.net/MrwanZH/article/details/76795235