當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。