本文整理匯總了Java中com.mysql.jdbc.jdbc2.optional.MysqlDataSource類的典型用法代碼示例。如果您正苦於以下問題:Java MysqlDataSource類的具體用法?Java MysqlDataSource怎麽用?Java MysqlDataSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MysqlDataSource類屬於com.mysql.jdbc.jdbc2.optional包,在下文中一共展示了MysqlDataSource類的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: testBug19169
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
/**
* Tests fix for BUG#19169 - ConnectionProperties (and thus some
* subclasses) are not serializable, even though some J2EE containers
* expect them to be.
*
* @throws Exception
* if the test fails.
*/
public void testBug19169() throws Exception {
MysqlDataSource toSerialize = new MysqlDataSource();
toSerialize.setZeroDateTimeBehavior("convertToNull");
boolean testBooleanFlag = !toSerialize.getAllowLoadLocalInfile();
toSerialize.setAllowLoadLocalInfile(testBooleanFlag);
int testIntFlag = toSerialize.getBlobSendChunkSize() + 1;
toSerialize.setBlobSendChunkSize(String.valueOf(testIntFlag));
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(bOut);
objOut.writeObject(toSerialize);
objOut.flush();
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(bOut.toByteArray()));
MysqlDataSource thawedDs = (MysqlDataSource) objIn.readObject();
assertEquals("convertToNull", thawedDs.getZeroDateTimeBehavior());
assertEquals(testBooleanFlag, thawedDs.getAllowLoadLocalInfile());
assertEquals(testIntFlag, thawedDs.getBlobSendChunkSize());
}
示例3: testBug16791
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
/**
* Tests fix for BUG#16791 - NullPointerException in MysqlDataSourceFactory
* due to Reference containing RefAddrs with null content.
*
* @throws Exception
* if the test fails
*/
public void testBug16791() throws Exception {
MysqlDataSource myDs = new MysqlDataSource();
myDs.setUrl(dbUrl);
Reference asRef = myDs.getReference();
System.out.println(asRef);
removeFromRef(asRef, "port");
removeFromRef(asRef, NonRegisteringDriver.USER_PROPERTY_KEY);
removeFromRef(asRef, NonRegisteringDriver.PASSWORD_PROPERTY_KEY);
removeFromRef(asRef, "serverName");
removeFromRef(asRef, "databaseName");
//MysqlDataSource newDs = (MysqlDataSource)
new MysqlDataSourceFactory().getObjectInstance(asRef, null, null, null);
}
示例4: initDatasource
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
public void initDatasource(YadaConfiguration config) throws NamingException {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setDatabaseName(config.getString("config/database/dbName"));
dataSource.setUser(config.getString("config/database/user"));
dataSource.setPassword(config.getString("config/database/password"));
dataSource.setServerName(config.getString("config/database/server"));
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("java:comp/env/jdbc/yadatestdb", dataSource);
super.dataSource = dataSource;
builder.activate();
// Database
Flyway flyway = new Flyway();
flyway.setLocations("filesystem:schema"); // Where sql test scripts are stored
flyway.setDataSource(dataSource);
flyway.clean();
flyway.migrate();
}
示例5: DataIntegrityTestTool
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
public DataIntegrityTestTool(Properties prop) throws Exception{
this.setName("data-integrity-test-thread");
srcDs = new MysqlDataSource();
srcDs.setURL(prop.getProperty(DMLQueryTool.PROP_MSR_SRC_URL));
srcDs.setUser(prop.getProperty(DMLQueryTool.PROP_MSR_SRC_USERNAME));
srcDs.setPassword(prop.getProperty(DMLQueryTool.PROP_MSR_SRC_PASSWROD));
destCount = Integer.parseInt(prop.getProperty(DMLQueryTool.PROP_MSR_DEST_COUNT));
destDs = new MysqlDataSource[destCount];
for(int i=0; i<destCount; i++){
destDs[i] = new MysqlDataSource();
destDs[i].setURL(prop.getProperty(String.format(DMLQueryTool.PROP_MSR_DEST_URL, i)));
destDs[i].setUser(prop.getProperty(String.format(DMLQueryTool.PROP_MSR_DEST_USERNAME, i)));
destDs[i].setPassword(prop.getProperty(String.format(DMLQueryTool.PROP_MSR_DEST_PASSWORD, i)));
}
}
示例6: beforeClass
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
@BeforeClass
public static void beforeClass() throws Exception{
initializeTestEnv();
final int sleepCount = 30;
LOGGER.debug("sleep " + sleepCount + "sec for generating enough test data");
for(int i=1; i<=sleepCount; i++){
LOGGER.debug(i + "/" + sleepCount);
Thread.sleep(1000);
}
// create dataSource
dataSource = new MysqlDataSource();
dataSource.setUrl(DB_URL);
dataSource.setUser(DB_USER);
dataSource.setPassword(DB_PW);
}
示例7: setUp
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
public void setUp() {
MysqlDataSource mySQLDataSource = new MysqlDataSource();
mySQLDataSource.setUser(SupportDatabaseService.DBUSER);
mySQLDataSource.setPassword(SupportDatabaseService.DBPWD);
mySQLDataSource.setURL("jdbc:mysql://localhost/test");
String envName = "java:comp/env/jdbc/MySQLDB";
SupportInitialContextFactory.addContextEntry(envName, mySQLDataSource);
ConfigurationDBRef config = new ConfigurationDBRef();
Properties properties = new Properties();
properties.put("java.naming.factory.initial", SupportInitialContextFactory.class.getName());
config.setDataSourceConnection(envName, properties);
databaseDSConnFactory = new DatabaseDSConnFactory((ConfigurationDBRef.DataSourceConnection) config.getConnectionFactoryDesc(), config.getConnectionSettings());
}
示例8: 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;
}
示例9: testBug19169
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
/**
* Tests fix for BUG#19169 - ConnectionProperties (and thus some
* subclasses) are not serializable, even though some J2EE containers
* expect them to be.
*
* @throws Exception if the test fails.
*/
public void testBug19169() throws Exception {
MysqlDataSource toSerialize = new MysqlDataSource();
toSerialize.setZeroDateTimeBehavior("convertToNull");
boolean testBooleanFlag = !toSerialize.getAllowLoadLocalInfile();
toSerialize.setAllowLoadLocalInfile(testBooleanFlag);
int testIntFlag = toSerialize.getBlobSendChunkSize() + 1;
toSerialize.setBlobSendChunkSize(String.valueOf(testIntFlag));
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(bOut);
objOut.writeObject(toSerialize);
objOut.flush();
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(bOut.toByteArray()));
MysqlDataSource thawedDs = (MysqlDataSource)objIn.readObject();
assertEquals("convertToNull", thawedDs.getZeroDateTimeBehavior());
assertEquals(testBooleanFlag, thawedDs.getAllowLoadLocalInfile());
assertEquals(testIntFlag, thawedDs.getBlobSendChunkSize());
}
示例10: testBug16791
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
/**
* Tests fix for BUG#16791 - NullPointerException in MysqlDataSourceFactory
* due to Reference containing RefAddrs with null content.
*
* @throws Exception if the test fails
*/
public void testBug16791() throws Exception {
MysqlDataSource myDs = new MysqlDataSource();
myDs.setUrl(dbUrl);
Reference asRef = myDs.getReference();
System.out.println(asRef);
removeFromRef(asRef, "port");
removeFromRef(asRef, NonRegisteringDriver.USER_PROPERTY_KEY);
removeFromRef(asRef, NonRegisteringDriver.PASSWORD_PROPERTY_KEY);
removeFromRef(asRef, "serverName");
removeFromRef(asRef, "databaseName");
//MysqlDataSource newDs = (MysqlDataSource)
new MysqlDataSourceFactory().getObjectInstance(asRef, null, null, null);
}
示例11: getDataSourceLocator
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
@Override
public SQLDataSourceLocator getDataSourceLocator() {
return new SQLDataSourceLocator() {
public DataSource lookup(SQLDataSetDef def) throws Exception {
String url = connectionSettings.getProperty("url");
String user = connectionSettings.getProperty("user");
String password = connectionSettings.getProperty("password");
MysqlDataSource ds = new MysqlDataSource();
ds.setURL(url);
if (!StringUtils.isBlank(user)) {
ds.setUser(user);
}
if (!StringUtils.isBlank(password)) {
ds.setPassword(password);
}
return ds;
}
};
}
示例12: before
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
@Before
public void before() throws IOException {
tempLocation = Files.createTempDirectory("jpagen").toFile();
System.out.println(tempLocation.getAbsolutePath());
com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds
= new MysqlDataSource();
ds.setUrl("jdbc:mysql://localhost/devopsplus");
ds.setUser("devopsplus");
ds.setPassword("devopsplus");
DBI dbi = new DBI(ds);
Handle handle = dbi.open();
config = Configuration.fromStream(getClass().getResourceAsStream("/test-2.json"));
reflector = new ReflectorMySql(config, handle);
}
示例13: 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);
}
示例14: getDataSource
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; //導入依賴的package包/類
private static DataSource getDataSource(Driver driver, String url, String user, String password) {
DataSource dataSource;
switch (driver) {
case H2:
JdbcDataSource h2DataSource = new JdbcDataSource();
h2DataSource.setUrl(url);
user = user != null ? user : "";
password = password != null ? password : "";
h2DataSource.setUser(user);
h2DataSource.setPassword(password);
return h2DataSource;
case MYSQL:
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setURL(url);
mysqlDataSource.setUser(user);
mysqlDataSource.setPassword(password);
return mysqlDataSource;
default:
throw new IllegalArgumentException("Unknown database driver");
}
}