-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Init the nosql client for first redis.
- Loading branch information
zhoumf
committed
Dec 19, 2013
1 parent
56a53d3
commit 433bc6e
Showing
70 changed files
with
8,806 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* DerbyClient.java | ||
* kevin 2013-2-20 | ||
* @version 0.1 | ||
*/ | ||
package org.kevin.db.derby; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
import org.kevin.redis.data.vo.Host; | ||
|
||
/** | ||
* @author kevin | ||
* @since jdk1.6 | ||
*/ | ||
public class DerbyClient { | ||
|
||
private static void testHostsAll() { | ||
final String seplite = ", "; | ||
DerbyDAO derbyStore = DerbyDAO.getInstance(); | ||
// derbyStore.dropTable(); | ||
// derbyStore.createTable(); | ||
Connection conn = derbyStore.getConnection(); | ||
derbyStore.execute("insert into hosts(ip, pwd, port) values('localhost', 'pwd', 6379)",conn); | ||
derbyStore.execute("insert into hosts(ip, pwd, port) values('127.0.0.2', 'pwd', 6380)",conn); | ||
derbyStore.execute("insert into hosts(ip, pwd, port) values('127.0.0.3', ' ', 6381)",conn); | ||
derbyStore.execute("insert into hosts(ip, pwd, port) values('127.0.0.4', 'pwd', 6382)",conn); | ||
// derbyStore.execute("insert into hosts values(1, 'localhost', 'pwd', 6379)", conn); | ||
// derbyStore.execute("insert into hosts values(2, '127.0.0.2', 'pwd', 6380)", conn); | ||
// derbyStore.execute("insert into hosts values(3, '127.0.0.3', 'pwd', 6381)", conn); | ||
// derbyStore.execute("insert into hosts values(4, '127.0.0.4', 'pwd', 6382)", conn); | ||
// System.out.println(conn); | ||
ResultSet rs = derbyStore.query("select * from hosts", conn); | ||
try { | ||
while (rs.next()) { | ||
// read the result set | ||
System.out.print("["); | ||
System.out.print("id=" + rs.getInt("id") + seplite); | ||
System.out.print("ip=" + rs.getString("ip") + seplite); | ||
System.out.print("pwd=" + rs.getString("pwd") + seplite); | ||
System.out.print("port=" + rs.getInt("port")); | ||
System.out.print("]"); | ||
System.out.print("\r\n"); | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
derbyStore.close(conn); | ||
} | ||
|
||
private static void testDBList() { | ||
List<Host> hosts = DerbyDB.getInstance().gethosts(); | ||
for (Host host : hosts) { | ||
System.out.println("[id:" + host.getId() + ", ip:" + host.getIp() + ", pwd:" + host.getPwd() + ", port:" + host.getPort() + "]"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
testHostsAll(); | ||
testDBList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* DerbyDAO.java | ||
* kevin 2013-2-20 | ||
* @version 0.1 | ||
*/ | ||
package org.kevin.db.derby; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.Properties; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
/** | ||
* @author kevin | ||
* @since jdk1.6 | ||
*/ | ||
public class DerbyDAO { | ||
|
||
public static String driver = "org.apache.derby.jdbc.EmbeddedDriver"; | ||
public static String protocol = "jdbc:derby:"; | ||
|
||
Properties props; | ||
|
||
Logger logger = Logger.getLogger(DerbyDAO.class); | ||
|
||
public DerbyDAO() { | ||
init(); | ||
} | ||
|
||
private static class DerbyDAOHolder { | ||
static DerbyDAO instance = new DerbyDAO(); | ||
} | ||
|
||
public static DerbyDAO getInstance() { | ||
return DerbyDAOHolder.instance; | ||
} | ||
|
||
private void init() { | ||
try { | ||
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); | ||
logger.info("Load the embedded driver"); | ||
Connection conn = null; | ||
props = new Properties(); | ||
props.put("user", "kevin"); | ||
props.put("password", "edison"); | ||
// create and connect the database named nosql | ||
//conn = DriverManager.getConnection("jdbc:derby:nosql;create=true", props); | ||
conn = DriverManager.getConnection("jdbc:derby:nosql;create=true"); | ||
logger.info("create and connect to nosql"); | ||
// System.out.println("create and connect to nosql"); | ||
Statement s = conn.createStatement(); | ||
try { | ||
String checkhosts = "select 1 from hosts"; | ||
s.execute(checkhosts); | ||
} catch (SQLException e) { | ||
String sql = "create table hosts(id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) primary key, ip varchar(32), pwd varchar(32), port integer, dbindex integer)"; | ||
s.execute(sql); | ||
logger.error("hosts not existed, create it:"); | ||
//e.printStackTrace(); | ||
} | ||
//conn.setAutoCommit(false); | ||
} catch (InstantiationException e) { | ||
logger.error("InstantiationException:" + e.getCause().getMessage()); | ||
// e.printStackTrace(); | ||
} catch (IllegalAccessException e) { | ||
logger.error("IllegalAccessException:" + e.getCause().getMessage()); | ||
// e.printStackTrace(); | ||
} catch (ClassNotFoundException e) { | ||
logger.error("ClassNotFoundException:" + e.getCause().getMessage()); | ||
// e.printStackTrace(); | ||
} catch (SQLException e) { | ||
logger.error("SQLException:" + e.getCause().getMessage()); | ||
// e.printStackTrace(); | ||
} | ||
} | ||
|
||
public Connection getConnection() { | ||
Connection conn = null; | ||
try { | ||
//conn = DriverManager.getConnection("jdbc:derby:nosql;create=true", props); | ||
conn = DriverManager.getConnection("jdbc:derby:nosql;create=true"); | ||
//logger.info("create and connect to nosql"); | ||
logger.info("connect to db nosql"); | ||
//conn.setAutoCommit(false); | ||
} catch (SQLException e) { | ||
logger.error("SQLException:" + e.getCause().getMessage()); | ||
//e.printStackTrace(); | ||
} | ||
return conn; | ||
} | ||
|
||
|
||
public int execute(String sql, Connection connection) { | ||
Statement statement; | ||
int result = 0; | ||
try { | ||
statement = connection.createStatement(); | ||
statement.setQueryTimeout(30); | ||
result = statement.executeUpdate(sql); | ||
statement.close(); | ||
//result = statement.execute(sql); | ||
} catch (SQLException e) { | ||
logger.error("SQLException:" + e.getMessage()); | ||
} | ||
return result; | ||
} | ||
|
||
public void dropTable() { | ||
Connection conn = getConnection(); | ||
String sql = "drop table hosts"; | ||
execute(sql, conn); | ||
close(conn); | ||
} | ||
|
||
public void alertTable() { | ||
Connection conn = getConnection(); | ||
String sql = "alter table hosts add column dbindex integer default 0"; | ||
execute(sql, conn); | ||
close(conn); | ||
} | ||
|
||
public ResultSet query(String sql, Connection connection) { | ||
Statement statement; | ||
ResultSet result = null; | ||
try { | ||
statement = connection.createStatement(); | ||
statement.setQueryTimeout(30); | ||
result = statement.executeQuery(sql); | ||
} catch (SQLException e) { | ||
logger.error("SQLException:" + e.getNextException()); | ||
} | ||
return result; | ||
} | ||
|
||
public int delete(String sql, Connection connection) { | ||
Statement statement; | ||
int result = 0; | ||
try { | ||
statement = connection.createStatement(); | ||
statement.setQueryTimeout(30); | ||
result = statement.executeUpdate(sql); | ||
} catch (SQLException e) { | ||
logger.error("SQLException:" + e.getCause().getMessage()); | ||
//e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
|
||
public void close(Connection conn) { | ||
try { | ||
if (conn != null && !conn.isClosed()) { | ||
conn.close(); | ||
} | ||
} catch (SQLException e) { | ||
logger.error("SQLException:" + e.getCause().getMessage()); | ||
//e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* DerbyDB.java | ||
* kevin 2013-2-20 | ||
* @version 0.1 | ||
*/ | ||
package org.kevin.db.derby; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.kevin.redis.data.vo.Host; | ||
import org.kevin.redis.msg.PopMessage; | ||
|
||
/** | ||
* @author kevin | ||
* @since jdk1.6 | ||
*/ | ||
public class DerbyDB { | ||
|
||
private static Connection conn = null; | ||
private static DerbyDB derbyDB = null; | ||
|
||
private DerbyDB(){ | ||
} | ||
|
||
public static DerbyDB getInstance(){ | ||
if(null == derbyDB) | ||
derbyDB = new DerbyDB(); | ||
return derbyDB; | ||
} | ||
|
||
public List<Host> gethosts(){ | ||
DerbyDAO dbStore = DerbyDAO.getInstance(); | ||
if(null == conn) | ||
conn = dbStore.getConnection(); | ||
ResultSet rs = dbStore.query("select * from hosts", conn); | ||
List<Host> hostsList = new ArrayList<Host>(); | ||
Host host = null; | ||
try { | ||
if(null != rs){ | ||
while (rs.next()) { | ||
// read the result set | ||
host = new Host(); | ||
host.setId(rs.getInt("id")); | ||
host.setIp(rs.getString("ip")); | ||
host.setDbIndex(rs.getInt("dbIndex")); | ||
host.setPwd(rs.getString("pwd")); | ||
host.setPort(rs.getInt("port")); | ||
hostsList.add(host); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
//System.out.println("table is not existed"); | ||
//e.printStackTrace(); | ||
} | ||
return hostsList; | ||
} | ||
|
||
public void addHost(Host host){ | ||
DerbyDAO dbStore = DerbyDAO.getInstance(); | ||
if(null == conn) | ||
conn = dbStore.getConnection(); | ||
//ResultSet rs = dbStore.query("select ip, port, dbindex from hosts where ip='" + host.getIp() + "' and port=" + host.getPort() + "' and dbindex=" + host.getDbIndex(), conn); | ||
ResultSet rs = dbStore.query("desc hosts ", conn); | ||
if(null == rs) { | ||
PopMessage.popWarn("No host is existed"); | ||
dbStore.alertTable(); | ||
} else { | ||
try { | ||
PopMessage.popWarn("Host table desc:" + rs.findColumn("name")); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
try { | ||
if(rs.next()) | ||
dbStore.execute("update hosts set ip='" + host.getIp() + "', port=" + host.getPort() + ", pwd='" + host.getPwd() + "' where ip='" + host.getIp() + "' and port=" + host.getPort()+ "' and dbindex=" + host.getDbIndex(), conn); | ||
else{ | ||
//int hostSeq = 0; | ||
//ResultSet rseq = dbStore.query("select seq from SQLITE_SEQUENCE where name='hosts'", conn); | ||
//if(rseq.next()) hostSeq = rseq.getInt("seq"); | ||
//hostSeq += 1; | ||
//dbStore.execute("insert into hosts values(" + hostSeq + ", '" + host.getIp() + "', 'pwd', " + host.getPort() +")", conn); | ||
dbStore.execute("insert into hosts(ip, pwd, port, dbindex) values('" + host.getIp() + "', '" + host.getPwd() + "', " + host.getPort() + "', " + host.getDbIndex() +")", conn); | ||
} | ||
} catch (SQLException ex) { | ||
PopMessage.popError("DB Query exception Cause: " + ex.getMessage()); | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
public int removeHost(Host host){ | ||
DerbyDAO dbStore = DerbyDAO.getInstance(); | ||
if(null == conn) | ||
conn = dbStore.getConnection(); | ||
int result = dbStore.execute("delete from hosts where id=" + host.getId() + "", conn); | ||
// try { | ||
// conn.close(); | ||
// } catch (SQLException e) { | ||
// e.printStackTrace(); | ||
// } | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE hosts ( | ||
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) primary key, | ||
ip varchar(32) NOT NULL, | ||
pwd varchar(500), | ||
port INTEGER, | ||
addtime INTEGER default 0 | ||
); |
Oops, something went wrong.