select host,user from mysql.user;
delete from mysql.user where user='takahab';
select host,user from mysql.user;
grant insert,select on *.* to takahab@localhost identified by 'himitsu';
select host,user from mysql.user;
delete from mysql.user where user='takahab';
grant all privileges on *.* to takahab@localhost identified by 'himitsu' with grant option;
select host,user from mysql.user;
※参考
すべてのホスト(%)
grant all privileges on *.* to takahab@% identified by 'himitsu' with grant option;
ホストを制限
grant all privileges on *.* to takahab@'172.16.0.0/255.255.255.0' identified by 'himitsu' with grant option;
【テストDBを作る】
$mysql -u root -p
drop database test;
create database test;
use test;
create table customer(
num integer not null,
name varchar(20) not null,
addr varchar(20) not null,
constraint pk_customer primary key (num)
);
insert into customer (num, name, addr) values (1, 'aaaaaaaa', '090-0123-4001');
insert into customer (num, name, addr) values (2, 'bbbbbbbb', '090-0123-4002');
insert into customer (num, name, addr) values (3, 'cccccccc', '090-0123-4003');
select * from customer;
【テストアプリを作る】
import java.sql.*;
class DbTest {
public static void main(String[] args) {
int id;
String name;
try {
// JDBCドライバの登録
String driver = "com.mysql.jdbc.Driver";
// データベースの指定
String server = "localhost"; // MySQLサーバ ( IP または ホスト名 )
String dbname = "test"; // データベース名
// String url = "jdbc:mysql://" + server + "/" + dbname + "?useUnicode=true&characterEncoding=EUC_JP";
String url = "jdbc:mysql://" + server + "/" + dbname;
String user = "takahab"; // データベース作成ユーザ名
String password = "himitsu"; // データベース作成ユーザパスワード
Class.forName (driver);
// データベース接続
System.out.println( url );
Connection con = DriverManager.getConnection(url, user, password);
// テーブル照会
Statement stmt = con.createStatement ();
String sql = "SELECT * FROM test.customer";
ResultSet rs = stmt.executeQuery (sql);
// テーブル出力
while(rs.next()){
id = rs.getInt("num");
name = rs.getString("name");
System.out.println("ID:" + id);
System.out.println("名前:" + name);
}
// データベースクローズ
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
System.err.println("SQL failed.");
e.printStackTrace ();
} catch (ClassNotFoundException ex) {
ex.printStackTrace ();
}
}
}
【環境】
mysqlをダウンロード
http://dev.mysql.com/downloads/connector/j/
cd ~/sample
mkdir lib
cd lib
ln -s ../mysql-connector-java-5.1.25/mysql-connector-java-5.1.25-bin.jar mysql-connector-java-5.1.25.jar
ln -s ../mysql-connector-java-5.1.25/mysql-connector-java-5.1.25-bin.jar mysql-connector-java.jar
ln -s ../mysql-connector-java-5.1.25/mysql-connector-java-5.1.25-bin.jar mysql.jar
cd ..
javac DbTest.java
java -cp ./lib/mysql.jar:. DbTest
0 件のコメント:
コメントを投稿