JavaEE知識体系
5137 ワード
一、データベース MySql管理 起動サービス:net start mysql クローズサービス:net stop mysql ログインMySql:mysql-uユーザー名-pパスワード SQL文 2.1 DDL
2.1.1操作database
2.1.2操作テーブル
2.2 DML
2.3 DQL
2.3.1単一テーブルクエリ
2.3.2マルチテーブルクエリ
2.4 DCL
2.5 TCL JDBC+プリコンパイル対象//1.ドライバクラスを登録します.forName(“com.mysql.jdbc.Driver”);//2.接続接続接続の取得=DriverManagement.getConnection(“url”,“username”,“password”);//3.SQL実行プラットフォームPreparedStatementpstmt=conn.prepareStatement(「使用?処理されたsql文」)を作成する.pstmt.setXXX(パラメータ番号、パラメータ値);//4.SQL文executeUpdate()を実行し、execute()ResultSet rs=pstmt.executeQuery();//5.処理結果while(rs.next(){XXX value=rs.getXXX("フィールド名");}//6.リソースrs.close()を解放する.pstmt.close(); conn.close(); 接続プール 4.1 c 3 p 0接続プールの使用インポートjarパケット:データベースドライバパケット、c 3 p 0 jarパケット はプロファイルを提供します. ファイル名:c 3 p 0-config.xml ファイルの場所:クラスロードパスの下(srcの下) コードを作成し、接続プールCombooledDataSource=new CombooledDataSource()を使用します.Connection conn = dataSource.getConnectoin();//connを使用してデータベースconn.close()を操作します.
4.2 druid接続プールの使用 jarパケットのインポート:データベースドライバパケット、druid jarパケット プロファイルの提供 ファイル名:xxx.properties ファイルの場所:クラスロードパスの下に置くことを推奨する コード作成接続プールDataSource dataSource=DruidDataSourceFactoryを使用する.createDataSource(Propertiesオブジェクト);Connection conn = dataSource.getConnection();//connを使用してデータベースconn.close()を操作します. JdbcTemplate
5.1使用手順 jarパッケージのインポート:データベースドライバパッケージ、接続プールのjarパッケージ、JdbcTemplateのjarパッケージ 接続プールのプロファイルとツールクラスJdbcUtils(ツールクラスで接続プールオブジェクトを取得する方法を提供) を提供します.コードを作成し、JdbcTemplateを使用してSQL文JdbcTemplate jdbcTemplate=new JdbcTemplate(JdbcUtils.getDataSource();//JdbcTemplateの異なるメソッドを使用して、異なるSQL文 を実行します.
5.2一般的な方法
2.1.1操作database
-- 1. database
create database ;
-- 2. database
show databases;
show create database ;
-- 3. database
alter database character set
-- 4. database
drop database ;
-- 5. database
use ;
select database();
2.1.2操作テーブル
-- 1. table
create table (
[ ],
...
[ ]
);
-- 2. table
show tables;
show create table ;
desc ; --
-- 3. table
drop table ;
-- 4. table
-- 4.1
rename table to ;
-- 4.2
alter table add [ ];
-- 4.3
alter table modify [ ];
-- 4.4
alter table change [ ];
-- 4.5
alter table drop ;
2.2 DML
-- 1.
insert into ( 1, 2,...) values ( 1, 2,...);
-- 2.
update set 1= 1, 2= 2,... where ;
-- 3.
delete from where ;
2.3 DQL
2.3.1単一テーブルクエリ
-- 1.
select * from ;
select 1, 2,... from ;
select ifnull( , ) from ;
select 1+ 2, 3+100,... from ;
select 1 as 1, 2 2 from ;
-- 2. :>,=,<=,=,<>,like, between...and..., in(), and, or, not
select * from emp where salary > 5000;
select * from emp where ename like ' %'; -- % ; _
select * from emp where salary between 2000 and 10000;
select * from emp where dept_id in (10, 30);
-- 3. :order by desc ,asc
select * from emp order by age desc;
-- 4. : null
select count(*) from emp;
select sum(salary) from emp;
select avg(salary) from emp;
select max(salary) from emp;
select min(salary) from emp;
-- 5. :group by having
select dept_id, count(*) from emp group by dept_id having count(*) > 10;
-- 6. :limit ,
select * from emp limit 0, 5;
2.3.2マルチテーブルクエリ
-- 1. :
select * from emp e, dept d where e.dept_id = d.id;
select * from emp e inner join dept d on e.dept_id = d.id;
-- 2. : ,
select * from emp e left join dept d on e.dept_id = d.id;
select * from emp e right join dept d on e.dept_id = d.id;
-- 3. : ,
select * from emp where salary = (select max(salary) from emp);
select * from dept where id in (select dept_id from emp where salary > 10000);
select * from dept d, (select * from emp where salary > 10000) t where d.id = t.dept_id;
2.4 DCL
-- 1.
create user 'tom'@'%' identified by 'tom';
-- 2.
grant all on *.* to 'tom'@'%';
-- 3.
show grants for 'tom'@'%';
-- 4.
revoke delete on *.* from 'tom'@'%';
-- 5.
drop user 'tom'@'%';
-- 6.
set password for 'tom'@'%' = password('1234');
2.5 TCL
-- 1.
start transaction; -- set autocommit = 0;
-- 2. DML
-- ......
-- 3. :
commit;
-- 3. :
rollback;
4.2 druid接続プールの使用
5.1使用手順
5.2一般的な方法
//1. , :
Integer value = jdbcTemplate.queryForObject("sql", Integer.class, params);
//2. , JavaBean
List list = jdbcTemplate.query("sql",new BeanPropertyRowMapper<>(JavaBean .class), params);
//3. , JavaBean
JavaBean obj = jdbcTemplate.queryForObject("sql",new BeanPropertyRowMapper<>(JavaBean .class),params);