Generate liquibase change log using java - java

I am trying to output a changelog xml file through java.
I am not sure as to what I need to pass as parameters to the following method.
liquibase.generateChangeLog("chris", changeLogWriter, outputStream, snapshotTypes);
Chris is the schema name in Oracle 11g XE.
I don't want to generate on command line. I want to use application I am building to generate it.
public class Test {
public static void main(String[] args) {
String driverName = "oracle.jdbc.driver.OracleDriver";
String dbURL = "jdbc:oracle:thin:#localhost:1521:xe";
String userName = "chris";
String userPwd = "Liberty123";
try {
Class.forName(driverName);
Connection c = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("success");
Liquibase liquibase = null;
try {
Database liqui_oracle = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(c));
liquibase = new Liquibase("", new FileSystemResourceAccessor(), liqui_oracle);
liquibase.generateChangeLog("chris", changeLogWriter, outputStream, snapshotTypes);

Related

How to implement Configurations class in Java?

I am using this example to read from configuration file (data such as host name, password, etc) . But they did not include the Configurations class itself.
So I am not really sure how that should be implemented.
Here is how I am trying to read the properties from Main class:
Configurations configs = new Configurations(); // Error: cannot find symbol symbol: class Configurations location: class Main
try {
Configuration config = configs.properties(new File("database.properties"));
String dbHost = config.getString("database.host");
int dbPort = config.getInt("database.port");
String dbUser = config.getString("database.user");
String dbPassword = config.getString("database.password", "secret"); // provide a default
long dbTimeout = config.getLong("database.timeout");
} catch (ConfigurationException cex) {
cex.printStackTrace();
}
And this is how my database.properties file looks:
database.host = "dbname";
datatabase.port = 5005;
datatabase.user = "root";
datatabase.password = "";
database.timeout = 60000
P.S. Sorry for my stupidity, I am very new to Java.
You can use the properties class in java, which has a load method that specifies an inputstream.
Then, you can read your properties file via FileInputStream.
example:
public class Test {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
InputStream inputStream =
new FileInputStream("D:\\work_space\\java_workspace\\test-mq\\src\\main\\resources\\database.properties");
properties.load(inputStream);
String host = properties.getProperty("database.host");
// get more properties......
System.out.println(host);
}
}

Setting up a remote Derby DB: "No suitable driver found" error

I am trying to set up a remote Derby database just for practice. The following code works without a problem whenever I access the DB on my harddrive:
class Test{
public static void main(String[] args) {
String protocol = "jdbc:derby:";
// String dbPath = "C:/Java_Practice/derbyDB"; // this dbPath works...
String dbPath = "//108.167.141.127/derbyDB"; // and this one doesn't
String url = protocol + dbPath;
try( Connection conn = DriverManager.getConnection(url) )
{
System.out.println(conn);
}
catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
I then uploaded the whole derbyDB directory to my Hostgator-hosted website, obtained its IP by pinging the server and edited the dbPath var accordingly. The code stopped working as if it can't even see the DB. What am I missing?
Looks like your driver class not loaded.
Try loading it before calling DriverManager.getConnection, and see if it works.
String driver = "org.apache.derby.jdbc.ClientDriver";
Class.forName(driver).newInstance();
String protocol = "jdbc:derby:";
String dbPath = "//108.167.141.127/derbyDB"+";create=true";
String url = protocol + dbPath;
Connection conn = DriverManager.getConnection(url);
Answering my own question after some digging.
This is what I found in the official Apache Derby documentation (https://db.apache.org/derby/docs/10.0/manuals/develop/develop14.html):
You can specify only databases that are local to the machine on which
the JVM is running.
Looks like what I wanted to accomplish cannot be done...

Linking an Access database to NetBeans IDE 8.02 without using JDBC-ODBC

How do I link my database to NetBeans without using jdbc.odbc? When I run the program it doesn't link.
public void connect() {
try {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String database1 = "jdbc:odbc:database1";
con = DriverManager.getConnection(database1);
st = con.createStatement();
}
}
Register you ms-access file (*.accdb) as a odbc data source.
http://www.mundayweb.com/progs/jdbc-odbc-tut.php

How can I read from MySQL database in Java

I need to read from a database. I'm under Windows. I get this error message:
java.sql.SQLException: No suitable driver found for localhost
This is an extract from my code:
public class JavaApplication10 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Properties connInfo = new Properties();
connInfo.put("characterEncoding","UTF8");
connInfo.put("user", "root");
connInfo.put("password", "goskomstat");
conn = DriverManager.getConnection("localhost", connInfo);
Being in Windows command line I can type:
C:\Program Files\MySQL\MySQL Server 5.6\bin>mysql -u root -p
Then I enter the password 'goskomstat' and can operate my databases.
What can I try next?
You need to define your connection as follows:
conn = DriverManager.getConnection("jdbc:mysql://localhost/", connInfo);
That allows you to specify the JDBC driver by using the specific URI prefix.

Java jTDS Connection Problems Ubuntu Server

From what I have read the correct connection string for a jTDS is:
jdbc:jtds:<server_type>://<server>[:<port>][/<database>]
I believe the issue is the server name. The server name is formatted like this
servername\adhoc
An SQLException gets thrown anytime I try to connect saying "unknown server host name"
Is that my issue, or is there something else I need to look into as well...?
import java.sql.*;
public class Main {
// The JDBC Connector Class.
private static final String MSdbClassName = "net.sourceforge.jtds.jdbc.Driver";
private static final String MSHOST = "servername\\adhoc"; //cascrmeufosqlp1\adhoc
private static final String MSDATABASE = "tier2";
private static final String MSUSER = "feed_****2";
private static final String MSPASSWORD = "*******0";
public static void main(String[] args) throws ClassNotFoundException,SQLException
{
Class.forName(MSdbClassName);
String url2 = "jdbc:jtds:sqlserver://" + MSHOST + ":1433/" + MSDATABASE;
Connection c2 = java.sql.DriverManager.getConnection( url2, MSUSER, MSPASSWORD );
System.out.println("MS SQL works...");
c2.close();
}
}
It looks like you are trying to connect to a "named instance" of sql server. You will need to use the "instance" property in the url. Something like this might work:
jdbc:jtds:sqlserver://servername:1433/dbName;appName=MyAPP;instance=instanceName
See the jTDS faq for more information found here: http://jtds.sourceforge.net/faq.html

Categories

Resources