Java DataSource Access Database __c#

Source: Internet
Author: User
Tags access database

In the past, we got the connection object by DriverManager this class.

Now we get connection through DataSource,

The rest is the same thing,

First, DataSource gets the connection code.

Before that, test the contents of the Db.properties configuration file in the first place

Jdbc.datasource=com.mchange.v2.c3p0.combopooleddatasource
Jdbc.driverclass=com.mysql.jdbc.driver
Jdbc.jdbcurl=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=
jdbc.maxpoolsize=100
jdbc.minpoolsize=2
jdbc.initialpoolsize=2
jdbc.acquireincrement=2
jdbc.maxstatements=1000
jdbc.maxidletime=300
jdbc.checkouttimeout=5000

The connection's explanation is all on the code annotation,

Package org.test;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

Import java.util.Properties;

Import Javax.sql.DataSource;

Import Org.apache.commons.beanutils.BeanUtils;

	public class Jdbcdatasourcetest {private static DataSource DataSource;

	private static Connection Connection;

	private static PreparedStatement PS;

	private static ResultSet Rs;

		public static void Main (string[] args) throws Exception {Properties Properties = new properties (); Loads data from the specified configuration file into the properties Properties.load (JdbcDataSourceTest.class.getClassLoader (). getResourceAsStream ("

		
		Db.properties ")); /** * Properties One of the records is like this * Jdbc.datasource=com.mchange.v2.c3p0.combopooleddatasource * will be preceded by JDBC. Remove the reload in a proper

		Ties/Properties Dbproperties = new properties ();

			For (Object Key:properties.keySet ()) {String temp = (string) key;
			if (Temp.startswith ("jdbc."))

				{String name = temp.substring (5); Dbproperties.puT (name, Properties.getproperty (temp));
		
		}//List all properties in Dbproperties dbproperties.list (System.out);

		System.out.println ("=============================================="); Loads its object dataSource = (dataSource) class.forname (Properties.getproperty ("Jdbc.datasource")) from the vendor-supplied DataSource implementation.

		
		Newinstance ();

		
		
		Add the contents of the Dbproperties property to the DataSource object Beanutils.populate (DataSource, dbproperties);

		Connection = Datasource.getconnection ();

		PS = connection.preparestatement ("SELECT * from Test");

		rs = Ps.executequery ();
		while (Rs.next ()) {System.out.println (rs.getstring (1));
 }
	}
}




And then comes the code that used to get connection through DriverManager.


Package org.test;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

public class Jdbctest
{
	
	private static Connection Connection;
	
	private static PreparedStatement PS;
	
	private static ResultSet Rs  ;
	
	Static 
	{
		try
		{
			class.forname ("Com.mysql.jdbc.Driver");
		}
		catch (ClassNotFoundException e)
		{
			e.printstacktrace ();
		}
		
		
	}
	
	public static void Main (string[] args) throws Exception
	{
		connection = drivermanager.getconnection ("JDBC: Mysql://localhost:3306/test "," Root "," "");
		
		PS = connection.preparestatement ("SELECT * from Test");
		
		rs = Ps.executequery ();
		
		while (Rs.next ())
		{
			System.out.println (rs.getstring (1));
		}
	}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.