jdbc由浅到深
根据提示,连贯记忆,可对jdbc进行初步掌握,之后熟练了即可尽情发挥,可以结合方法:接下来跟我一起走一遍吧:
package JDBC.util;
import java.sql.*;
public class Test01Query {
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//1.加载驱动
Class.forName(“com.mysql.jdbc.Driver”);
//2.获得连接 通过驱动管理器连接数据库
String url = “jdbc:mysql://localhost:3306/hospital”;
String user = “root”;
String password = “199652”;
connection = DriverManager.getConnection(url, user, password);
//3.创建一个容器 或者获得语句
statement = connection.createStatement();
//4.写sql语句查询该表数据
String sql = “select *from student”;
//5.执行容器中的sql语句
resultSet = statement.executeQuery(sql);
//6.遍历处理结果集
while (resultSet.next()) {
int id = resultSet.getInt(1);
System.out.println(id);
String name = resultSet.getString(“name”);
System.out.println(name);
String sex = resultSet.getString(“sex”);
System.out.println(sex);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
以上是查询数据库的jdbc,那么增删改呢?还是有点区别的,他们三个有个共同点,在执行sql语句返回结果集时,他们的写法是
resultSet =statement.executeUpdate(sql);
那么,熟练了之后可以怎么写呢?可以写进方法里面
package dao;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BaseDao {
private static String driver = “com.mysql.jdbc.Driver”;
private static String url = “jdbc:mysql://localhost:3306/sql?user=root&password=199652”;
Connection conn = null;
public Connection getConnection() {
if(conn==null){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
}
return conn;
}
public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public int executeUpdate(String sql, Object[] obj) {
PreparedStatement pstmt = null;
int num = 0;
conn = getConnection();
try {
pstmt = conn.prepareStatement(sql);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
pstmt.setObject(i + 1, obj[i]);
}
}
num = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closeAll(conn, pstmt, null);
}
return num;
}}
再之后呢,等学了hibernate 等框架,就可以将jdbc进行封装,他们底层依然是jdbc,不过进行封装后就显得特别轻量
来一个:
<session-factory>
<!-- 配置关于数据库连接的四个项 driverClass url username password -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">199652</property>
<!-- 设置连接提供者 -->
<!-- <property name="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property> -->
<!-- c3p0连接池的配置 -->
<!-- <property name="hibernate.c3p0.max_size">20</property> --> <!-- 最大连接池 -->
<!-- <property name="hibernate.c3p0.min_size">5</property> --> <!-- 最小连接数 -->
<!-- <property name="hibernate.c3p0.timeout">120</property> --> <!-- 超时 -->
<!-- <property name="hibernate.c3p0.idle_test_period">3000</property> --> <!-- 空闲连接 -->
<!-- 可以将向数据库发送的sql显示出来 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- 用于设置事务提交方式 -->
<property name="hibernate.connection.autocommit">false</property>
<!-- 配置hibernate的映射文件所在位置 -->
<mapping resource="com/sram/entity/User.hbn.xml" />
</session-factory>
有没有发现什么呢?