发表于: 2017-10-01 23:10:44
1 836
今天完成的事情
maven在使用中央仓库时由于服务器架设在国外,这里我就使用了阿里云的镜像maven库。
使用java连接数据库这里先练习了最基本的jdbc jdbc又是什么呢?
导入必须的mysql驱动包后建立连接
因为在driver类中还有静态代码块
所以优化成正常的连接方式
为了方便测试 少写重复代码 这里对JDBC的连接和关闭做了一个抽取
public class JdbcUtils {
public static Connection getConnection() throws Exception{
String url = "jdbc:mysql://localhost:3306/mydemo";
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(url,"root","8866521");
}
public static void closeConnection(Statement statement,Connection connection){
if(statement!=null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
这里在创建JdbcUtils时,这里涉及了原来学习java过程中的一个薄弱知识点就是static关键字,这里我在方法名前加入static进行修饰,那么我在调用此方法时,不需要将JdbcUtils进行实例化,直接使用类名.方法名。
public class testconnection {
@Test
public void test3() throws Exception {
Connection connection =JdbcUtils.getConnection();
Statement statement = connection.createStatement();
//String sql = "insert into user(id,name) VALUES(26666,'我是张三')";
//String sql = "delete from user where name like '%正%'";
String sql ="update user set name='王某某' where id=266";
statement.executeUpdate(sql);
JdbcUtils.closeConnection(statement,connection);
}
}
这里再次练习普通增删改功能,where判定条件加了一个模糊查询
“%”代表任意字符;
“_"代表单个字符;
进行查询时,对返回的数据进行取出时 用两种方法,第一根据索引取值,但第一种方法极不推荐,原因是得根据数据列索引数,如超过10列那么将非常麻烦且易出错,这里我采用了第二种方法 根据列名取值。
这里顺便强化查询sql训练
查询学号为10号和15号同学的日志数量,并显示他们的姓名,学号,日志总和
@Test
public void test4() throws Exception{
Connection connection =JdbcUtils.getConnection();
Statement statement = connection.createStatement();
String sql = "" +
"select u.id,u.name,COUNT(d.id) 'cont' " +
"from user u,daily d " +
"where u.id=d.to_user_id and u.id in (10,15) " +
"GROUP BY u.id" +
";";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
int id = resultSet.getInt("id") ;
String name = resultSet.getString("name");
String count =resultSet.getString("cont");
System.out.println("学号是"+id+"的"+name+"一共写了"+count+"日志");
}
}
在关联查询时,一定要注意消除笛卡尔基现象
然后尝试使用preparestatement
@Test
public void test5() throws Exception {
Connection connection =JdbcUtils.getConnection();
String sql = "delete from user where id=?";
PreparedStatement p = connection.prepareStatement(sql);
//下标从1开始!!!!
p.setInt(1,20);
p.executeUpdate();
JdbcUtils.colseConnection(p,connection);
}
}
这里注意如sql语句中需要加入具体的值时,先用通配符?代替,在预编译后,使用set注入数据,但起始的下标是从1开始,之后执行executeUpdate或executeQuery ,当然了如果sql语句没有数据就无需set注入,返回值依旧一样,增删改返回影响行数 int 类型, 查询返回ResultSet对象。
statement 在执行后每次都会新建一个sql语句,而preparestatement则会在数据库缓冲区留下好比上面的sql delete from user where id=?" 语句,在下次不改变语句情况下直接去数据库缓冲区找到该语句赋予新的值,在频繁访问数据库时推荐使用,但目前mysql不支持数据库缓冲,而orcale和 sql service 支持
如我们在页面登录 密码验证时 使用statement 访问数据库 存在被注入风险,如密码为 XXXX or 1=1,那么查询语句想变成select * from user where id=xxx and password=XXX or 1=1,那么这句话就等于select * from user 安全风险高,所以使用preparestatement.较好
这里应为修改pom.xml文件导致JDK回到1.5出现了好多麻烦 找到解决方法
在pom文件中设定jdk版本即可,以下这种写法会自动更新idea中的LanguageLevel和JavaCompiler
1. <build>
2. <plugins>
3. <plugin>
4. <groupId>org.apache.maven.plugins</groupId>
5. <artifactId>maven-compiler-plugin</artifactId>
6. <version>2.3.2</version>
7. <configuration>
8. <source>1.8</source>
9. <target>1.8</target>
10. </configuration>
11. </plugin>
12. </plugins>
13. </build>
在操作数据过程中,连接打开,操作完毕,连接关闭,频繁的操作影响运行效率,那么就引入了连接池这个概念。
预先创建一组连接,用的时候取出一个,用完后放回连接池,大大提高了效率。
且在Spring框架中是默认支持C3P0连接池技术的,在spring jdbc 中也需要此技术。
@Test
public void test1() throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl( "jdbc:mysql://localhost:3306/mydemo");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setUser("root");
dataSource.setPassword("8866521");
//初始化连接个数
dataSource.setInitialPoolSize(4);
//最大连接个数
dataSource.setMaxPoolSize(8);
//单个连接最长空闲时间
dataSource.setMaxIdleTime(1000);
Connection connection = dataSource.getConnection();
String sql = "select * from user u where u.id between 80 and 90";
PreparedStatement p = connection.prepareStatement(sql);
ResultSet resultSet = p.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getInt("id")+resultSet.getString("name"));
}
resultSet.close();
p.close();
connection.close();
}
当然了还有配置xml文件的方法
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydemo</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">8866521</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
<property name="maxIdleTime">1000</property>
</default-config>
<named-config name="oracle_config">
<property name="jdbcUrl">orcale的url</property>
<property name="driverClass">orcale的驱动</property>
<property name="user">XXX</property>
<property name="password">XXX</property>
<property name="initialPoolSize">3</property>
<property name="maxPoolSize">6</property>
<property name="maxIdleTime">1000</property>
</named-config>
</c3p0-config>
这里特意多配了一个Orcale的数据库连接源,这里我要严重吐槽下maven项目中c3p0-config.xml这个配置文件一定要放在src/main/resources目录下而不是src目录下,空指针报错纠结好长时间。
@Test
public void test2() throws Exception{
// 自动加载src/main/resources 下c3p0的配置文件【c3p0-config.xml】!
ComboPooledDataSource dataSource = new ComboPooledDataSource();// 括号里不传参数用默认数据库,传参使用<named-config name="XXX">name中的名称
Connection connection = dataSource.getConnection();
String sql = "delete from user where id = ?";
PreparedStatement p = connection.prepareStatement(sql);
p.setInt(1,80);
p.executeUpdate();
p.close();
connection.close();
}
OK那么暂时告一段落,开始spring的正式学习,昨天说了很清楚时间不够了 放个喜欢的框架上去而已。
Spring主要核心是:
(1).控制反转(IOC):以前传统的java开发模式中,当需要一个对象时我们,我们会自己使用new或者getInstance等直接或者间接调用构造方法创建一个对象,而在Spring开发模式中,Spring容器使用了工厂模式为我们创建了所需要的对象,我们使用时不需要自己去创建,直接调用Spring为我们提供的对象即可,这就是控制反转的思想。
(2).依赖注入(DI):Spring使用java Bean对象的Set方法或者带参数的构造方法为我们在创建所需对象时将其属性自动设置所需要的值的过程就是依赖注入的基本思想。
(3).面向切面编程(AOP):在面向对象编程(OOP)思想中,我们将事物纵向抽象成一个个的对象。而在面向切面编程中,我们将一个个对象某些类似的方面横向抽象成一个切面,对这个切面进行一些如权限验证,事物管理,记录日志等公用操作处理的过程就是面向切面编程的思想。
在Spring中,所有管理的对象都是JavaBean对象,而BeanFactory和ApplicationContext就是spring框架的两个IOC容器,现在一般使用ApplicationnContext,其不但包含了BeanFactory的作用,同时还进行更多的扩展。
实例化Spring IOC容器的简单方法:
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class testspring {
@Test
public void test1(){
Resource resource = new ClassPathResource("src/main/resources/applicationContext.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
beanFactory.getBean("emp");
}
}
但是因maven项目路径的问题 无法读取到xml配置文件 测试失败。时间已晚,暂时收工。
明日计划:加速推动task1的完成,主要还是spring基础框架搭建完后对使用jdbc template,和再回顾学习mybatis,分析两者差别。
今日问题:对idea和maven不熟悉引出的jdk版本重置到1.5的问题,及spring配置文件路径读取问题。
收获:在maven中利用阿里云maven库加快下包速度,回顾基础jdbc 连接基础,C3P0连接池,和加强sql查询语句锻炼。把spring框架单独拎出来学习的一个切入点。
评论