java数据库连接

49 篇文章 2 订阅
11 篇文章 0 订阅

一.JDBC连接

 Class.forName("com.mysql.jdbc.Driver");
   	     Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bumen?characterEncoding=utf8","root","123456");
   	     Statement stat=conn.createStatement();
   	     String sql="select password from information where account='xx'";
	     ResultSet rs=stat.executeQuery(sql);

二. properties

(1)jdbc.properties文件

#
jdbc.drivername=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/employees?characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
(2)读出 jdbc.properties文件
package com.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class PropertiesUtil {
  
	private static Properties pro=new Properties();
    
	static{
		InputStream inStream=PropertiesUtil.class.getResourceAsStream("/com/jdbc/jdbc.properties");
		try {
			pro.load(inStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String getPropertyValue(String key){
		return pro.getProperty(key);
	}
}
(3)得到相应的连接值

package com.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBUtil {
   private static String drivername;
   private static String url;
   private static String accouont;
   private static String password;
   
   static{
		drivername=PropertiesUtil.getPropertyValue("jdbc.drivername");
		url=PropertiesUtil.getPropertyValue("jdbc.url");
		accouont=PropertiesUtil.getPropertyValue("jdbc.username");
		password=PropertiesUtil.getPropertyValue("jdbc.password");
	}
   
public static Connection getConnection() throws ClassNotFoundException, SQLException{
	Connection conn;
	Class.forName(drivername);
	conn=DriverManager.getConnection(url,accouont,password);
	return conn;
}   
   
}
(4)调用getConnection()方法返回一个对象conn然后进行sql操作

三:Cp30连接池(mchange-commons-java-0.2.11包)

package com.jdbc;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0 {

	 public Connection getC3poCnn() throws PropertyVetoException, SQLException{
		 ComboPooledDataSource ds=new ComboPooledDataSource();
		 ds.setDriverClass("com.mysql.jdbc.Driver");
		 ds.setJdbcUrl("jdbc:mysql://localhost:3306/employees?characterEncoding=utf8");
		 ds.setUser("root");
		 ds.setPassword("123456");
		 
		 ds.setInitialPoolSize(30);
		 ds.setMaxPoolSize(60);
		 ds.setMaxIdleTime(30);
		 ds.setMinPoolSize(60);
	
		 return ds.getConnection();
	 }
	 
	 public static void main(String args[]) throws PropertyVetoException, SQLException{
		 Connection conn=new C3p0().getC3poCnn();
		 Statement stat=conn.createStatement();
		 ResultSet rs=stat.executeQuery("select * from employees");
		 while(rs.next()){
			 System.out.println(rs.getString(2));
		 }
	 }
}

四.spring框架数据库连接

(1).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.xsd">
     <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="ds">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/employees?characterEncoding=utf8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
     </bean> 
     
     
     
     
</beans>
(2)连接类

package com.java.awt;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0 {

	 public Connection getC3poCnn() throws PropertyVetoException, SQLException{
		 ApplicationContext context=
		 new ClassPathXmlApplicationContext("spring.xml");
		 ComboPooledDataSource ds=(ComboPooledDataSource)context.getBean("ds");
		//运行上面这句时就主动把值传入ComboPooledDataSource中的set方法,xml中的name对应set方法
		 ds.setInitialPoolSize(30);
		 ds.setMaxPoolSize(60);
		 ds.setMaxIdleTime(30);
		 ds.setMinPoolSize(60);
	
		 return ds.getConnection();
	 }
	 
	 public static void main(String args[]) throws PropertyVetoException, SQLException{
		 Connection conn=new C3p0().getC3poCnn();
		 Statement stat=conn.createStatement();
		 ResultSet rs=stat.executeQuery("select * from employees");
		 while(rs.next()){
			 System.out.println(rs.getString(2));
		 }
	 }
}


四.spring.xml引入外部文件连接,看import引入对应的包

<context:property-placeholder location="classpath:jdbc.properties" />//外部文件的引入(BeanFactory后置处理器)


<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> //通过$直接可以取到相应的变量
     <property name="driverClassName" value="${jdbc.driverName}"></property>
     <property name="url" value="${jdbc.url}"></property>
     <property name="username" value="${jdbc.username}"></property>
     <property name="password" value="${jdbc.password}"></property>
</bean>

  <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
  </bean>
测试类
package JDBClink;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class Connetion {
	
         public static void main(String args[]){
        	 ApplicationContext cxt=new ClassPathXmlApplicationContext("JDBC.springconfig.xml");
        	 JdbcTemplate jdbcTemplate=(JdbcTemplate)cxt.getBean("jdbcTemplate");
        	 System.out.println(jdbcTemplate);;
        	 String sql=" insert into emp(id,name,sex,age,addr) values(?,?,?,?,?)";
        	 int i=jdbcTemplate.update(sql,2,"zhangsan","男",19,"广东广州");
     		 System.out.println(i);
 
         }

}







  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值