本文整理匯總了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.
}
}
示例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;
}
示例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);
}
示例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();
}
示例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();
}
}
示例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();
}
}
示例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();
}
}
示例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;
}
示例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();
}
示例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();
}