星期日, 6月 03, 2012
星期五, 6月 01, 2012
[Mysql] Host ‘yourhost’ is not allowed to connect to this MySQL server錯誤
連線MySQL時,出現標題上的錯誤訊息,
這個問題是此IP位址未授權給MYSQL
解決方法:直接在下SQL語法設定即可這個問題是此IP位址未授權給MYSQL
補充:
進入設定檔 改bind-address
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
1.登入mysql
shell>mysql -u root -p
mysql>use mysql
2.設定資料庫的使用權限
允許所有連線
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘MySQL帳號’@'%’ IDENTIFIED BY ‘MySQL密碼’ WITH GRANT OPTION;
Tip:% 代表不限client的連線ip
設定特地連結的ip
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘MySQL帳號’@'你要連線IP‘ IDENTIFIED BY ‘MySQL密碼’ WITH GRANT OPTION;
3.最後一定要更新權限
mysql>FLUSH PRIVILEGES;
Reference:
[MySQL]解決ERROR 1130: Host ‘yourhost’ is not allowed to connect to this MySQL server錯誤無法遠端連接MySQL:message from server: "Host xxx is not allowed to connect to this MySQL server"
MySQL的重要語法
[Java] DBCP Sample
簡單的DBCP範例,主要記錄Reference物件的用法
Setting up initial context for DBCP connection pool.
Setting up initial context for DBCP connection pool.
星期四, 5月 31, 2012
[Java] Cannot create JDBC driver of class '' for connect URL 'null'
想要透過local的專案直接連mysql所遇到的
Cannot create JDBC driver of class '' for connect URL 'null'錯誤訊息的方法,
Google到的資訊實在太多做法了,把試成功的經驗記錄一下。
step1:在專案內的WEB-INF/加入context.xml檔
Tip:DBCP已經設全域設定了,所以這個不用加
step2:在專案的web.xml加入
<resource-ref>
<description>DB Connectiondescription>
<res-ref-name>jdbc/資料表連接名稱res-ref-name>
<res-type>javax.sql.DataSourceres-type>
resource-ref>
Cannot create JDBC driver of class '' for connect URL 'null'錯誤訊息的方法,
Google到的資訊實在太多做法了,把試成功的經驗記錄一下。
step1:在專案內的WEB-INF/加入context.xml檔
<xml version="1.0" encoding="UTF-8"?>
<Context >
<Resource
name="jdbc/資料表連接名稱"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="密碼"
driverClassName="com.mysql.jdbc.Driver"
autoReconnect="true"
removeAbandoned="true"
removeAbandonedTimeout="300"
logAbandoned="false"
url="jdbc:mysql://遠端IP:3306/資料庫名稱"/>
Context>
Tip:DBCP已經設全域設定了,所以這個不用加
非常重要!!記得要重新開Tomcat!!
星期一, 3月 05, 2012
[Java] 在JDBC使用SQL IN
這篇文章Batching Select Statements in JDBC提供四種解法
擷錄前言:
In networking, one of the most expensive things you can do is make a server roundtrip. In JDBC, this corresponds to a database call. If you are doing inserts, updates or deletes, you can use the executeBatch() signature to cut down the number of trips to the server. Unfortunately, there is no such built-in mechanism for select queries.
Suppose you want to get the names for a given list of ids. Logically, we would want to do something like:
擷錄前言:
In networking, one of the most expensive things you can do is make a server roundtrip. In JDBC, this corresponds to a database call. If you are doing inserts, updates or deletes, you can use the executeBatch() signature to cut down the number of trips to the server. Unfortunately, there is no such built-in mechanism for select queries.
Suppose you want to get the names for a given list of ids. Logically, we would want to do something like:
PreparedStatement stmt = conn.prepareStatement(
"select id, name from users where id in (?)");
stmt.setString("1,2,3");
However, this will not work. JDBC only allows you to substitute a single literal value in the "?". You can't substitute things that are meant to become part of the SQL statement. This is necessary because if the SQL itself can change, the driver can't precompile the statement. It also has the nice side effect of preventing SQL injection attacks.
Instead, you have four options for the implementation:
- Run the query separately for each id
- Build one query to do everything
- Use a stored procedure
- Select batching
最後選擇效能最快的一種。但尚未測試時間差多少倍。
StringBuilder inClause = new StringBuilder();
boolean firstValue = true;
for (int i=0; i < batchSize; i++) {
inClause.append('?');
if ( firstValue ) {
firstValue = false;
} else {
inClause.append(',');
}
}
PreparedStatement stmt = conn.prepareStatement(
"select id, name from users where id in (" + inClause.toString() + ')');
for (int i=0; i < batchSize; i++) {
stmt.setInt(i); // or whatever values you are trying to query by
}
星期二, 2月 21, 2012
[Java] PreparedStatement Order by 失效問題!!
使用PreparedStatement操作Order by指令無法得到排序的解法!!
參考Stackoverflow:PreparedStatement not returning ordered ResultSet 至一文。
範例:
透過PreparedStatement來執行以下sql子句
參考Stackoverflow:PreparedStatement not returning ordered ResultSet 至一文。
範例:
透過PreparedStatement來執行以下sql子句
星期四, 2月 09, 2012
[Java] Insert if not exist: 資料列不存在時才新增
重覆的連絡人清單就不要插入!!
重點在select * from (select 2,'ken') as tmpInsert記得要重新給一個別名,
不然編譯都會爆炸給你看lol
範例:
插入一個使用者ken到聯絡人清單索引為2的結果
insert into contact_list_member(contact_list_id,uid)
select * from (select 2,'ken') as tmpInsert
where not exists(select 1 from contact_list_member clm where clm .contact_list_id= 2 and clm .uid='ken')
使用preparedstatement就會像下面那樣
//insert if not exist
String insertMemberSql=
"insert into contact_list_member(contact_list_id,uid) select * from (select ?,?) as tmpInsert where not exists(select * from contact_list_member where contact_list_id=? and uid=?)";
Update 2012/03/08 今天在同事的測試下發現上面的語句在插入的值都一樣的時候所產生的duplicate column name bug,簡單的用新增使用者的範例來記綠一下
範例:
以下語句要新增一個帳號跟密碼都是叫cindy,不一樣就不會有這個問題!!
insert into users(account,password) select* from (select 'cindy','cindy') as tmpInsert where not exists(select 1 from users where account = 'cindy')
問題: 錯誤的原因在於我們要建立一個帳號跟密碼一樣的使用者,會造成表格出現重覆欄位名稱的問題(Duplicate column name 'cindy'錯誤)
以下是產生這個錯誤的SQL片段
select* from (select 'cindy','cindy') as tmpInsert 解法
因tmpInsert已經含有重覆的column name了,因此我們需要指定tmpInsert內的column name,才不會讓表格內的欄位名稱重覆的異常
select account ,password from (select 'cindy' as account ,'cindy' as password) as tmpInsert
上述語句會產生的tmpInsert資料表結果集
account | password
cindy | cindy
Reference: MySQL一個聰明的sql,當Record沒有的時候才增加
不存在时才插入数据
星期二, 2月 07, 2012
[Java] 必學!! 取得當前新增資料的索引值
當在MySQL使用AUTO_INCREMENT產生索引的值,如何取得新增的索引值呢?
MySQL提供了LAST_INSERT_ID()解決這個問題!!
SELECT LAST_INSERT_ID();
如果要用JDBC實作的話只需要加入Statement.RETURN_GENERATED_KEYS,
執行getGeneratedKeys()即可輕鬆取得!!
如果要用JDBC實作的話只需要加入Statement.RETURN_GENERATED_KEYS,
執行getGeneratedKeys()即可輕鬆取得!!
星期日, 2月 05, 2012
[Java] JDBC Transaction
本篇說明如何在JDBC實作Transaction機制。
步驟一:MsSQL設定:
設定INNODB資料表
Tip:MySQL 對不同的 Table 可以選擇不同的 Implementation 方式,像是最早的 ISAM (ISAM 將在 MySQL 5.0 以後被移除),後來的 MyISAM (因為要做 Transaction 時要預留多一點空間來進行處理,因此如果不須使用 Transaction 時,直接建 Table 就會採用 MyISAM 的格式)、把 Table 存在記憶體中提升效能的 HEAP Table,以及有支援 Transaction 的 INNODB 格式。
步驟二:如何在Java code執行Transcation
步驟一:MsSQL設定:
設定INNODB資料表
Tip:MySQL 對不同的 Table 可以選擇不同的 Implementation 方式,像是最早的 ISAM (ISAM 將在 MySQL 5.0 以後被移除),後來的 MyISAM (因為要做 Transaction 時要預留多一點空間來進行處理,因此如果不須使用 Transaction 時,直接建 Table 就會採用 MyISAM 的格式)、把 Table 存在記憶體中提升效能的 HEAP Table,以及有支援 Transaction 的 INNODB 格式。
步驟二:如何在Java code執行Transcation
星期日, 9月 25, 2011
星期四, 6月 23, 2011
[Java] Read Microsoft XLS using JDBC
import java.sql.*;
public class MainWindow {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=d:/desktop/test.xls");
Statement st=con.createStatement();
ResultSet rs= st.executeQuery("select * from [Sheet1$]");//Sheet1是工作表名稱
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("name"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Reference:
Apache POI - Code Sample
Handle Excel files
星期六, 4月 23, 2011
[JAVA] 快快樂樂學JAVA JDBC 連接 MySQL
JDBC連接MySQL
使用JDBC連接資料庫存取資料時,必須執行以下三個步驟:
1.用DriverManager載入及註冊適當的JDBC驅動程式
如果發生Driver not found錯誤訊息改改用
2.用JDBC URL定義驅動程式與資料來源之間的連結,並且建立一個連結物
3.建立一個sql陳述句,並利用它來執行SQL語法
執行陳述句有三種方法:
方法一:取回結果集
方法二:新增、更新、刪除等等使用
使用JDBC連接資料庫存取資料時,必須執行以下三個步驟:
1.用DriverManager載入及註冊適當的JDBC驅動程式
Class.forName("com.mysql.jdbc.Driver");
如果發生Driver not found錯誤訊息改改用
Class.forName("com.mysql.jdbc.Driver").newInstance();
2.用JDBC URL定義驅動程式與資料來源之間的連結,並且建立一個連結物
//option1 String jdbcUrl = "jdbc:mysql://[hostname]:[port]/[dbname]?user=[username]&password=[pwd]"; Connection conn = DriverManager.getConnection(jdbcUrl);
//option2 String jdbcUrl = ""jdbc:mysql://[hostname]:[port]/[dbname]"; Connection conn = DriverManager.getConnection(jdbcUrl,"username","password");
3.建立一個sql陳述句,並利用它來執行SQL語法
Statement stmt = conn.createStatement();
執行陳述句有三種方法:
方法一:取回結果集
ResultSet rs = stmt.executeQuery("sql語法")
//取得每一列資訊
while(rs.next()){
//取得這筆資料的結果
rs.getString(1);
rs.getInt(2);
}
方法二:新增、更新、刪除等等使用
//updateRow為執行成功的列數
int updateRow = stmt.excuteUpdate("sql語法");
方法三:單純判斷執行有無成功使用,回傳boolean
bool success = stmt.execute("sql語法");
訂閱:
意見 (Atom)