Java EEとmysql Springデータベース開発の例1

JdbcTemplateTest.java

パッケージcom.itheima.jdbc;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
パブリッククラスJdbcTemplateTest {
public static void main(String [] args){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext( "applicationContext.xml");
JdbcTemplate jdTemplate =
(JdbcTemplate)applicationContext.getBean( "jdbcTemplate");
jdTemplate.execute( "create table account(" +
"id int primary key auto_increment、" +
"username varchar(50)、" +
"balance double)");
System.out.println( "

}

applicationContext.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org / 2001 / XMLSchema-instance "
xsi:schemaLocation =" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
<! -1配置データソース->
<bean id = "dataSource" class =
"org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-数データ库驱动->
<property name = "driverClassName" value = "com .mysql.jdbc.Driver "/>
<!-连接数データ库的url- >
<property name =" url "value =" jdbc:mysql:// localhost:3306 / spring "/>
<!
-データベースに接続するためのユーザー名
- > <プロパティ名= "username"値= "root" /> <!-データベースに接続するためのパスワード->
<property name = "password" value = "root" />
</ bean>
<!-2配置JDBC模板->
<bean id = "jdbcTemplate"
class = "org.springframework.jdbc.core.JdbcTemplate">
<!-认认数数データソース->
<property name = "dataSource" ref = "dataSource" />
</ bean>
</ beans>

 

 

おすすめ

転載: www.cnblogs.com/1314-520/p/12705811.html