HiSEN

Personal Technology Blog


  • 归档

  • 分类

  • 标签

  • 书单

  • 关于

  • 搜索
close
HiSEN

MongoDB - 简单操作 - CRUD - JAVA

发表于 2017-07-11 | 分类于 sql

之前我记得写过简单的测试类,但是忘了,现在重新写一个

顺便用博客记录下,mongodb系列应该会有几篇记录

本篇具体代码:SampleMongoTestNo1.java

1、环境介绍

mongodb安装教程:点击查看

1
2
3
4
5
6
7
8
DataBase:MongoDB V 3.2
Driver :3.4.1
maven:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.1</version>
</dependency>

2、初始化连接

1
2
3
4
5
6
7
8
9
10
11
12
13
private Mongo mg = null;
private DB db;
private DBCollection dbCollection;

@Before
public void initDB(){
//建立连接
mg = new MongoClient("127.0.0.1",27017);
//获取要操作的数据库实例,没有会创建
db = mg.getDB("hisen");
//获取要操作的集合实例,没有会创建
dbCollection = db.getCollection("emp");
}

3、关闭连接

1
2
3
4
5
6
7
8
9
@After
public void destoryDB(){
if (mg == null) {
mg.close();
mg=null;
db=null;
dbCollection=null;
}
}

4、CRUD操作

阅读全文 »

HiSEN

浏览器出现:Reimage Repair - zh.reimageplus.com

发表于 2017-07-05 | 分类于 软件

近期使用chrome出现打开一些网站老是弹出莫名其妙的网址

最终指向的都是Reimage Repair,网址zh.reimageplus.com
Reimage Repair
网上一查,貌似因为插件被污染的原因

我关闭chrome所有的插件,一个一个排查,最后找到了一个插件出问题

本来还想去举报,发现这个插件被下架了

问题排查

1
2
3
右上角菜单按钮 ---更多工具 --- 扩展程序 --- 关闭所有插件
然后一个个打开,测试某些网页是否还会继续跳出那恶心的东西
然后就找到问题了。

HiSEN

ubuntu root su 密码

发表于 2017-07-04 | 分类于 linux

在安装完ubuntu的时候

只有自己设置的非root的帐号和密码

但是又要用root密码怎么办呢?

1
2
3
4
hisen@ubuntu-1:~$ sudo passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

上面输入的密码就是你的root密码

检测一下

1
2
3
hisen@ubuntu-1:~$ su
Password:
root@ubuntu-1:/home/hisen#

通过,至此结束

HiSEN

Ubuntu安装RocketMQ - Quick Start

发表于 2017-07-02 | 分类于 linux

安装需求:

  1. 64位的系统 Linux/Unix/Mac
  2. 64bit JDK 1.8+;
  3. Maven 3.2.x;
  4. Git (一般自带)

如果还未安装jdk、maven建议查看教程:点击查看

Clone & Build

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
git clone -b develop https://github.com/apache/incubator-rocketmq.git
cd incubator-rocketmq
mvn -Prelease-all -DskipTests clean install -U
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Apache RocketMQ 4.2.0-incubating-SNAPSHOT .......... SUCCESS [01:07 min]
[INFO] rocketmq-remoting 4.2.0-incubating-SNAPSHOT ........ SUCCESS [ 15.749 s]
[INFO] rocketmq-common 4.2.0-incubating-SNAPSHOT .......... SUCCESS [ 10.243 s]
[INFO] rocketmq-client 4.2.0-incubating-SNAPSHOT .......... SUCCESS [ 11.638 s]
[INFO] rocketmq-store 4.2.0-incubating-SNAPSHOT ........... SUCCESS [ 13.108 s]
[INFO] rocketmq-srvutil 4.2.0-incubating-SNAPSHOT ......... SUCCESS [ 2.051 s]
[INFO] rocketmq-filter 4.2.0-incubating-SNAPSHOT .......... SUCCESS [ 3.917 s]
[INFO] rocketmq-broker 4.2.0-incubating-SNAPSHOT .......... SUCCESS [ 8.726 s]
[INFO] rocketmq-tools 4.2.0-incubating-SNAPSHOT ........... SUCCESS [ 6.002 s]
[INFO] rocketmq-namesrv 4.2.0-incubating-SNAPSHOT ......... SUCCESS [ 2.726 s]
[INFO] rocketmq-logappender 4.2.0-incubating-SNAPSHOT ..... SUCCESS [ 3.514 s]
[INFO] rocketmq-openmessaging 4.2.0-incubating-SNAPSHOT ... SUCCESS [ 2.668 s]
[INFO] rocketmq-example 4.2.0-incubating-SNAPSHOT ......... SUCCESS [ 2.390 s]
[INFO] rocketmq-filtersrv 4.2.0-incubating-SNAPSHOT ....... SUCCESS [ 2.145 s]
[INFO] rocketmq-test 4.2.0-incubating-SNAPSHOT ............ SUCCESS [ 5.428 s]
[INFO] rocketmq-distribution 4.2.0-incubating-SNAPSHOT .... SUCCESS [ 27.002 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:07 min
[INFO] Finished at: 2017-07-02T01:35:40+08:00
[INFO] Final Memory: 60M/247M
[INFO] ------------------------------------------------------------------------
cd distribution/target/apache-rocketmq

Start Name Server

1
2
3
nohup sh bin/mqnamesrv &
tail -f ~/logs/rocketmqlogs/namesrv.log
The Name Server boot success...

Start Broker

1
2
3
nohup sh bin/mqbroker -n localhost:9876 &
tail -f ~/logs/rocketmqlogs/broker.log
The broker[%s, 172.30.30.233:10911] boot success...

Send & Receive Messages

1
2
3
4
5
6
7
Before sending/receiving messages, we need to tell clients the location of name servers. RocketMQ provides multiple ways to achieve this. For simplicity, we use environment variable NAMESRV_ADDR
export NAMESRV_ADDR=localhost:9876
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
SendResult [sendStatus=SEND_OK, msgId= ...

sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
ConsumeMessageThread_%d Receive New Messages: [MessageExt...

Shutdown Servers

1
2
3
4
5
6
7
sh bin/mqshutdown broker
The mqbroker(36695) is running...
Send shutdown request to mqbroker(36695) OK

sh bin/mqshutdown namesrv
The mqnamesrv(36664) is running...
Send shutdown request to mqnamesrv(36664) OK

HiSEN

powerdesigner Oracle mysql comment 缺少右括号

发表于 2017-06-22 | 分类于 sql

powerdesigner Oracle mysql comment 缺少右括号

今天人家给我个powerdesigner设计好的表

要我去间数据库,直接复制出来去oracle执行,结果报错。

在comment附近,如果把comment去掉则可以正确执行

最后找到罪魁祸首:powerdesigner没有设置对数据库

解决办法,在powerdesigner页面

1
database-->change curren DBMS

上面是设置你需要的数据库

下面是为更改前的数据库

HiSEN

sql行转列,列转行

发表于 2017-06-13 | 分类于 sql

有时候需要行转列或者列转行

在Oracle 11g中,Oracle 又增加了2个查询:pivot(行转列) 和unpivot(列转行)

下面是在mysql中的操作:

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
mysql> select * from test;
+------+------+--------+
| id | age | weigth |
+------+------+--------+
| 1 | 1 | 3 |
| 1 | 2 | 2 |
| 1 | 3 | 1 |
| 2 | 5 | 6 |
+------+------+--------+
4 rows in set (0.00 sec)

#根据ID分组
mysql> select group_concat(age) from test group by id;
+-------------------+
| group_concat(age) |
+-------------------+
| 1,2,3 |
| 5 |
+-------------------+
2 rows in set (0.01 sec)

#不分组
mysql> select group_concat(age) from test;
+-------------------+
| group_concat(age) |
+-------------------+
| 1,2,3,5 |
+-------------------+
1 row in set (0.00 sec)

HiSEN

oracle全局查找某字段所在的表

发表于 2017-06-12 | 分类于 sql

下面这个存储过程是查找数据库中某个字段值为:hisen 的表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
declare
l_cnt varchar2(20);
v_sql varchar2(4000);
v_tablename varchar(200);
cursor cursor_jsdx is select 'select count(*) from ' || table_name || ' where NAME=''hisen''',table_name from user_tab_columns where column_name='NAME';
--注:这里的字段名要大写
begin
open cursor_jsdx;
Loop
fetch cursor_jsdx into v_sql,v_tablename;
exit when cursor_jsdx%notfound;
execute immediate v_sql into l_cnt;
if l_cnt >0 then
---如果该表有那内容的就打印那个表的名字。
dbms_output.put_line(v_tablename);
end if;
end loop;
Close cursor_jsdx;
end;

HiSEN

java.sql.SQLException Connections could not be acquired from the underlying database

发表于 2017-06-02 | 分类于 java

java.sql.SQLException: Connections could not be acquired from the underlying database

这个错误出现的原因有的说是因为jdbc配置文件写错了

正确的写法是:

1
2
3
4
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test
c3p0.user=root
c3p0.password=root

而不是

1
2
3
4
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root

但是我遇到的不是如此

后来发现是因为maven出问题了,jar包没有加载进项目

这是在starkoverflow上看到的回答

1
In my case it was problem that c3p0-0.9.2.1.jar file was not copied by maven into src/main/webapp/web-inf/libs

最后maven重新install之后就解决了

HiSEN

尝试使用NIO读取文件

发表于 2017-06-01 | 分类于 java

具体代码如下,这里获取出来的文件大小是准确的

FileInputStream方式获取出来的大小会有差异

NIO与IO的对比详见:NIOCopy.java

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
/**
* 利用NIO进行读写文件
*
* @param oldFileName 原文件的路径
* @param newFileName 新文件的路径
*/
public static void nioCopy(String oldFileName, String newFileName) {
try {
FileChannel fileChannelIn = new FileInputStream(new File(oldFileName)).getChannel();
FileChannel fileChannelOut = new FileOutputStream(new File(newFileName)).getChannel();
//获取文件大小
long size = fileChannelIn.size();
System.out.printf("文件大小为:%s byte \n",size);
//缓冲
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

long start = System.currentTimeMillis();
while (fileChannelIn.read(byteBuffer) != -1) {
//准备写
byteBuffer.flip();
fileChannelOut.write(byteBuffer);
//准备读
byteBuffer.clear();
}
long end = System.currentTimeMillis();
System.out.printf("NIO方式复制完成,耗时 %s 秒\n",(end-start)/1000);
//关闭
fileChannelIn.close();
fileChannelOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

HiSEN

try redis - redis官方教程练习

发表于 2017-05-19 | 分类于 sql

这里主要是介绍了几种redis支持的数据结构,以及操作方法

官网地址:http://try.redis.io/

我的redis是安装在linux虚拟机,通过Xshell操作,显示可能跟cmd不大一样

但是操作都是一样的

具体操作如下:

阅读全文 »

1…151617…27
hisenyuan

hisenyuan

Java R & D

266 日志
33 分类
112 标签
GitHub Weibo
Links
  • 科技爱好者周刊
  • 美团技术团队
  • duanple(老师木)
  • 当然我在扯淡(王垠)
  • 段永平的博客
  • 梦殇国际
© 2016 - 2024 hisenyuan
由 Hexo 强力驱动
您是第  个访问者    |   
主题 - NexT.Mist