当前位置: 首页>>代码示例>>Java>>正文


Java MysqlDataSource.getConnection方法代码示例

本文整理汇总了Java中com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection方法的典型用法代码示例。如果您正苦于以下问题:Java MysqlDataSource.getConnection方法的具体用法?Java MysqlDataSource.getConnection怎么用?Java MysqlDataSource.getConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.mysql.jdbc.jdbc2.optional.MysqlDataSource的用法示例。


在下文中一共展示了MysqlDataSource.getConnection方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: readTable

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
public void readTable(String user, String password, String server){
     MysqlDataSource dataSource = new MysqlDataSource();
     dataSource.setUser(user);
     dataSource.setPassword(password);
     dataSource.setServerName(server);
     try{
          Connection conn = dataSource.getConnection();
          Statement stmt = conn.createStatement();
          ResultSet rs = stmt.executeQuery("SELECT * FROM data_science.books");
          while (rs.next()){
               int id = rs.getInt("id");
               String book = rs.getString("book_name");
               String author = rs.getString("author_name");
               Date dateCreated = rs.getDate("date_created");
               System.out.format("%s, %s, %s, %s\n", id, book, author, dateCreated);
          }
          rs.close();
          stmt.close();
          conn.close();
     }catch (Exception e){
          //Your exception handling mechanism goes here.
     }
}
 
开发者ID:PacktPublishing,项目名称:Java-Data-Science-Cookbook,代码行数:24,代码来源:TestDB.java

示例2: getConnection

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
public static Connection getConnection() {
    if(url==null||user==null||password==null){
        initData();
    }
    try {
        if (conn == null || conn.isClosed()) {
            MysqlDataSource ds_mysql = new MysqlDataSource();
            ds_mysql.setUrl(url);// 设置连接字符串
            conn = ds_mysql.getConnection(user, password);
            return conn;
        }
    } catch (SQLException e) {
        System.out.println("连接JDBC出错!");
        e.printStackTrace();
        return null;
    }
    return conn;
}
 
开发者ID:leoliew,项目名称:meta-programming-jdbc,代码行数:19,代码来源:DBManager.java

示例3: init

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
public void init() throws SQLException, RuleBaseException, TeEngineMlException, ParserRunException
{
	ruleBaseName = "origdirt";
	MysqlDataSource dataSource = new MysqlDataSource();
	dataSource.setServerName("qa-srv");
	dataSource.setPort(3308);
	dataSource.setUser("db_readonly");
	//dataSource.setPassword("");
	distSimConnection = dataSource.getConnection();
	
	DistSimParameters originalDirtParameters =
		new DistSimParameters("original_dirt.od_templates", "original_dirt.od_rules", LIMIT_DISTSIM_RULES, 2*Constants.DEFAULT_DIRT_LIKE_RESOURCES_CACHE_SIZE, Constants.DEFAULT_DIRT_LIKE_RESOURCES_CACHE_SIZE);

	
	ruleBase = new DistSimRuleBase(distSimConnection,originalDirtParameters,ruleBaseName,parserMode);
	
	
	parser = ParserFactory.getParser(miniparParameter);
}
 
开发者ID:hltfbk,项目名称:Excitement-TDMLEDA,代码行数:20,代码来源:DynRuleBaseTester.java

示例4: testBug42267

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
public void testBug42267() throws Exception {
    MysqlDataSource ds = new MysqlDataSource();
    ds.setUrl(dbUrl);
    Connection c = ds.getConnection();
    String query = "select 1,2,345";
    PreparedStatement ps = c.prepareStatement(query);
    String psString = ps.toString();
    assertTrue("String representation of wrapped ps should contain query string", psString.endsWith(": " + query));
    ps.close();
    ps.toString();
    c.close();
}
 
开发者ID:bragex,项目名称:the-vigilantes,代码行数:13,代码来源:DataSourceRegressionTest.java

示例5: main

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
public static void main(String[] args) {

		// Define connection to the server
		MysqlDataSource dataSource = new MysqlDataSource(); // If you are not using MySQL, use different DataSource
		dataSource.setUrl("jdbc:mysql://relational.fit.cvut.cz"); // Public read-only database for testing
		dataSource.setUser("guest");
		dataSource.setPassword("relational");

		// Which database to analyse
		String databaseName = "mutagenesis"; // Another database to try: financial
		String schemaName = ""; // Always empty for MySQL databases

		// Connect to the database
		try (Connection connection = dataSource.getConnection()){

			// Estimate the PK and FK
			List<Table> tables = Schema.getPrimaryKeys(connection, databaseName, schemaName);
			tables = Optimization.optimize(tables);
			List<Relationship> relationships = Schema.getRelationships(connection, databaseName, schemaName, tables, false);
			OptimizationRelationship.optimize(relationships, tables);
			List<CompoundRelationship> compoundRelationships = CompoundRelationship.buildFrom(relationships);

			// Print the estimated PK and FK with the defined quote characters
			System.out.println("Estimated primary keys follow: ");
			System.out.println(SQL.getPkQuery(tables, '`', '`'));
			System.out.println("Estimated foreign key constraints follow: ");
			System.out.println(SQL.getFkQuery(compoundRelationships, '`', '`'));
		} catch (SQLException | InterruptedException e) {
			e.printStackTrace();
		}
	}
 
开发者ID:janmotl,项目名称:linkifier,代码行数:32,代码来源:Demo.java

示例6: run

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
private void run() throws IOException, SQLException {
    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + database);
    dataSource.setUser(username);
    dataSource.setPassword(password);

    try (FileInputStream inputStream = new FileInputStream(file)) {
        ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
        runner.setAutoCommit(true);
        runner.setStopOnError(true);
        runner.runScript(new InputStreamReader(inputStream));
        runner.closeConnection();
    }
}
 
开发者ID:syhily,项目名称:sql-executor,代码行数:15,代码来源:Command.java

示例7: doGet

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    MysqlDataSource ds = (MysqlDataSource) req.getServletContext().getAttribute("DBDataSource");

    try (Connection conn = ds.getConnection();
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT name, value FROM fruits ORDER BY value DESC limit 5")) {

        List<Fruit> fruits = new ArrayList<Fruit>();
        // Extract data from result set
        while (rs.next()) {
            //Retrieve by column name
            String name = rs.getString("name");
            int value = rs.getInt("value");
            // Add item
            fruits.add(new Fruit(name, value));
        }

        req.setAttribute("chartData", new Gson().toJson(fruits));
        req.setAttribute("chartTitle", "Top 5 fruits");
        req.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(req, resp);
    } catch (SQLException se) {
        se.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:anychart-integrations,项目名称:java-jsp-jdbc-mysql-template,代码行数:28,代码来源:MainServlet.java

示例8: getAntiExploitConnection

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
private Connection getAntiExploitConnection() {
	try {
		MysqlDataSource mySqlDataSource = new MysqlDataSource();
		mySqlDataSource.setDatabaseName(AntiExploit.getInstance().getConfiguration().getConfig().getDatabase());
		mySqlDataSource.setUser(AntiExploit.getInstance().getConfiguration().getConfig().getUsername());
		mySqlDataSource.setPassword(AntiExploit.getInstance().getConfiguration().getConfig().getPassword());
		mySqlDataSource.setServerName(AntiExploit.getInstance().getConfiguration().getConfig().getHost());
		mySqlDataSource.setPort(AntiExploit.getInstance().getConfiguration().getConfig().getPort());
		return mySqlDataSource.getConnection();
	} catch (SQLException ex) {
		AntiExploit.getInstance().getLogger().error("Failed to connect to MySQL database!");
		ex.printStackTrace();
	}
	return null;
}
 
开发者ID:LXGaming,项目名称:AntiExploit,代码行数:16,代码来源:MySQL.java

示例9: getConnection

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
public static Connection getConnection(String url, String username, String password) throws SQLException{
    MysqlDataSource ds = new MysqlDataSource();
    ds.setUrl(url);
    ds.setUser(username);
    ds.setPassword(password);
    return ds.getConnection();
}
 
开发者ID:kakao,项目名称:adt,代码行数:8,代码来源:HandlerTest.java

示例10: testBug42267

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //导入方法依赖的package包/类
public void testBug42267() throws Exception {
	MysqlDataSource ds = new MysqlDataSource();
	ds.setUrl(dbUrl);
	Connection conn = ds.getConnection();
	String query = "select 1,2,345";
	PreparedStatement ps = conn.prepareStatement(query);
	String psString = ps.toString();
	assertTrue("String representation of wrapped ps should contain query string", psString.endsWith(": " + query));
	ps.close();
	ps.toString();
	conn.close();
}
 
开发者ID:hinsenchan,项目名称:fil_project_mgmt_app_v2,代码行数:13,代码来源:DataSourceRegressionTest.java


注:本文中的com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。