Java jTDS Connection Problems Ubuntu Server - java

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

Related

2 datasource connection using properties file

My application.yml file looks something like:
spring:
profiles: environment
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate:
ddl-auto: none
datasource:
url: jdbc:mysql://myUrl
username: master
driver-class-name: "com.mysql.cj.jdbc.Driver"
My DatabaseConnection class looks something like:
#Component
public class DatabaseManager {
public static final String CONTEXT_KEY = "context";
public static final String CONTEXT_VALUE = "myContext";
public static final String TABLE_NAME = "my-credential-store";
public static final String CREDENTIAL = "mydb.password";
public static final String WRITE_ENDPOINT_OUTPUT = "mydbwriteendpoint";
public static final String DATASOURCE_URL = "spring.datasource.url";
public static final String DATASOURCE_PASSWORD = "spring.datasource.password";
public static void createConnection() throws Exception {
JCredStash jCredStash = null;
try {
Map<String, String> context = new HashMap<>();
context.put(CONTEXT_KEY, CONTEXT_VALUE);
jCredStash = new JCredStash(TABLE_NAME);
String secret = jCredStash.getSecret(CREDENTIAL, context);
String writeEndpoint = jCredStash.getSecret(WRITE_ENDPOINT_OUTPUT, context);
System.setProperty(DATASOURCE_URL,
"jdbc:mysql://" + writeEndpoint + ":port/mydb");
System.setProperty(DATASOURCE_PASSWORD, secret);
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (jCredStash != null)
jCredStash.close();
}
}
}
Here, I am getting the DB password from AWS Cred Stash. Everything is working fine for "mydb" database. Now I have created another database named "mydb1" inside the same URL instance with the same username and password. I have tried a couple of options like creating another data source, but nope of them worked for me. Can anyone guide me how can I create the connection with "mydb1" along with "mydb"? Thanks

How to open page on port 8080

Im trying to open a page using java and selenium on port 8080. Ive tried using the page and :8080 but the page continually keeps opening on a different port. Im basically trying to use zap and it configured to use firefox on port 8080. ANy help appreciated. Ive added my test and my class thats added to the test below, i think somewhere int he driver part it must call a different port but i cant see how this is happening
Test:
public class ZapScanTest {
static Logger log = Logger.getLogger(ZapScanTest.class.getName());
private final static String ZAP_PROXYHOST = "127.0.0.1";
private final static int ZAP_PROXYPORT = 8080;
private final static String ZAP_APIKEY = null;
// Change this to the appropriate driver for the OS, alternatives in the
drivers directory
private final static String FIREFOX_DRIVER_PATH =
"drivers/geckodriver.exe";
private final static String MEDIUM = "MEDIUM";
private final static String HIGH = "HIGH";
private ScanningProxy zapScanner;
private Spider zapSpider;
private WebDriver driver;
private Dec myApp;
private final static String[] policyNames = {"directory-browsing","cross-
site-scripting","sql-injection","path-traversal","remote-file-
inclusion","server-side-include",
"script-active-scan-rules","server-side-code-injection","external-
redirect","crlf-injection"};
int currentScanID;
#Before
public void setup() {
zapScanner = new
ZAProxyScanner(ZAP_PROXYHOST,ZAP_PROXYPORT,ZAP_APIKEY);
zapScanner.clear(); //Start a new session
zapSpider = (Spider)zapScanner;
log.info("Created client to ZAP API");
driver = DriverFactory. createProxyDriver("firefox",createZapProxyConfigurationForWebDriver(), FIREFOX_DRIVER_PATH);
myApp = new Dec(driver);
//myApp.registerUser(); //Doesn't matter if user already exists, bodgeit just throws an error
}
#After
public void after() {
driver.quit();
}
#Test
public void testSecurityVulnerabilitiesBeforeLogin()throws Exception {
myApp.login();
log.info("Spidering...");
spiderWithZap();
log.info("Spider done.");
setAlertAndAttackStrength();
zapScanner.setEnablePassiveScan(true);
scanWithZap();
List<Alert> alerts = filterAlerts(zapScanner.getAlerts());
logAlerts(alerts);
assertThat(alerts.size(), equalTo(0));
}
Dec class:
public class Sportdec {
WebDriver driver;
final static String BASE_URL = "https://web-game-stage.dec.com/games:8080";
final static String USERNAME = "dec2009#hotmail.com";
final static String PASSWORD = "tables";
public Dec(WebDriver driver) {
this.driver = driver;
this.driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
this.driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
}
public void login()throws Exception {
driver.get(BASE_URL);
Header header = new Header();
header.guest_select_login();
Pages.Login login = new Pages.Login();
login.login_with_empty_fields();
login.login_with_invalid_email();
login.email_or_password_incorrect();
login.login_open_and_close();
}
By any chance have you tried using this
final static String BASE_URL = "https://web-game-stage.dec.com:8080/games";
instead of this
final static String BASE_URL = "https://web-game-stage.dec.com/games:8080";
You are adding port number to the games directory instead of the 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...

Generate liquibase change log using 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);

Connect mysql with java

I am trying to connect to mysql in java (I use Wamp) so I used the following code : (I'm using Eclipse)
package genererPlanning;
import java.sql.*;
public class genererPlanning{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/mysql";
static final String USER = "root";
static final String PASS = "";
public static void main(String[] args){
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root", "");
}
catch (SQLException ex){
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}
But it returned me this text :
SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
SQLState: 08001
VendorError: 0
Do you know why it does not work?
PS : I looked at theses links but don't find my answer :
- stackoverflow
- stackoverflow
- stackoverflow
- commentcamarche
Thanks for all.
You need to use the mysql-connector.jar
Here's the link with instructions as to how to set it to your classpath.
http://code.google.com/p/find-ur-pal/downloads/detail?name=mysql-connector-java-5.1.18-bin.jar&
You need mysql jdbc connector..download from this site

Categories

Resources