????Table of Contents
????1??JDBC
????????????JDBC????壬???£???????????
????JDBC??Java Data Base Connectivity??java??????????????????????SQL????Java API????????????????????????????????????Java?????д?????????ɡ?JDBC??????????????????????????????????????????????????д???????ó???????JDBC???????????????????????????????????????????????DB????????????????????JDBC???б??????????????????DB????????????JDBC??????c3p0??
???????????????????????JDBC????DB???????????????
//1. ???DriverManager??DriverManager?е???List registeredDrivers ?????????е?Driver????
Class.forName("com.mysql.jdbc.Driver");
//2.????????????Э??/??Э??/?????????????????
String url = "jdbc:mysql://localhost:3306/csc?user=root&password=xxxx";
//3. DriverManager ???????????????????????????
Connection connection = DriverManager.getConnection(url);
//4. ???????????? (Statement??PrepareStatement(?????[???sql???]??????)??CallableStatement???洢?????)
//4.1 Statement
Statement statement = connection.createStatement();
//execute query
ResultSet resultSet = statement.executeQuery("SELECT  * FROM test ORDER by ID limit 10");
//4.2 PrepareStatement???????????????????????????
connection.setAutoCommit(false);
PreparedStatement preparedStatementInsert = connection.prepareStatement("INSERT  into test(name??sex) VALUES (????)");
preparedStatementInsert.setString(1?? "csophys");
preparedStatementInsert.setInt(2?? 0);
preparedStatementInsert.execute();
Statement getLastIdStatement = connection.createStatement();
ResultSet set = getLastIdStatement.executeQuery("SELECT LAST_INSERT_ID()");
connection.commit();
//4.3 statement ??batch????????CallableStatement????????
//5. ?????????ResultSet
while (resultSet.next()) {
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString("id"));
}
while(set.next()){
System.out.println(set.getString(1));
System.out.println(set.getString("LAST_INSERT_ID()"));
}
?????????????????????????????????JDBC????DB????????????衣??????????

???????? ???????????? Statement?????????????Statement??PrepareStatement??CallableStatement??CallableStatement???????洢????? ??Statement??PrepareStatement?????PrepareStatement????PrepareStatement ?????????SQL??????????sql???????????sql??? ??Statement??е??????????????????sql???????????????????????????sql?????PrepareStatement??Statement??Ч??????????PrepareStatement?????????SQL???
????2??DATASOURCE
????Datasource ??????DriverManager ???????????????????Connection?????DataSource???????DB??????????????????л??DB????
???????????DataSource??DriverManager?????????????????????DriverManager????????DB????????????????????????DB??????????
????????????????????????????????DB????????????DB?????????????儷????Datasource??????ε???????????Connection?????????
?????????????????
???????????Spring??Mybatis???м???????????????????????DataSource?????????????????c3p0??????£?
<!--c3p0?????-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/csc" />
<property name="user" value="root" />
<property name="password" value="xxxx" />
</bean>
????????????????????????????????????DataSource??????Mybatis??á????£?
<!--MyDatasource-->
<bean id="myDataSource" class="base.jdbc.MyDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/csc" />
<property name="user" value="root" />
<property name="password" value="xxxx" />
</bean>
????MyDataSource????????DB?????????????????????DriverManager?????DB?????MyDataSource??????javax.sql.DataSource??
????????????????getConnection????????????????????
public class MyDataSource implements DataSource {
private String driverClass;
private String jdbcUrl;
private String user;
private String password;
public Connection getConnection() throws SQLException {
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return DriverManager.getConnection(jdbcUrl+"?user="+user+"&password="+password);
}
....
??????????????DataSource?滻c3p0???????????????????????
????3????д????????????
?????????д?????????ж??????????????????????????????????????磬w??r1??r2.w?????r1??r2?????????????3???????wds??rds1??rds2?? ??????DBA???????????????????????????????????????????
??????????????????????????????????????SQL??????????????????????????????????????????????????????????????ο???????????????zebra????

????GroupPrepareStatement??е?????????SQL?????????????????????GroupConnection?е?Datasource???????????????connection???????????????
??????????????????????????????????????????ж?????????????????????з????е???????????sql?е??????????????????????DataSource??