Access SQL Server 2008 R2 database from netbeans 8 - java

I am completely new to java, and I want to make a connection to a remote SQL server 2008 R2 database (like 192.168.17.11) and load data from it.
Please suggest alternative ways if you know any.

First you must have JDBC driver for MS SQL, you have two jars (jtds-1.2.5.jar or sqljdbc4-2.0.jar), which added to your classpath
Second you need to create your connection as below:
String password="pass";
String driver= "net.sourceforge.jtds.jdbc.Driver"; // For sqljdbc4, use: com.microsoft.sqlserver.jdbc.SQLServerDriver
String username="user";
String URL="jdbc:jtds:sqlserver://serverIP:port/dbname"; // For sqljdbc4, use: jdbc:sqlserver://serverIP:port;databaseName=dbname
Class.forName(driver);
Connection conn = DriverManager.getConnection(URL, username, password);
// Use your connection here
// Don't forget to close the connection

Related

Java MySql Connection Through DSN

I am trying to connect to one of my MySql Databases through a System DSN I set up. The DSN is set up correctly with my SSL certs, username, password, port, and the databases populate the DSN database drop down and the "Test" connection passes. I can't seem to get a connection in Java. I have spent 2 days looking through some examples on Stack but they all refer to an Access database and using JDBC-ODBC bridge which is no longer available in Java 8. I tried using UCanAccess with Jackcess but I have gotten no where. The code below is what I have been tinkering with the last few hours. I normally connect to MySql databases with PHP and receive result in JSON or directly with JDBC driver but for this project neither are really an option. Any ideas. I appreciate the help.
//String username = "<username>";
//String password = "<password>";
//String database = "<database_name>";
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//Connect to cllients MySql Database
Connection conn = DriverManager.getConnection("jdbc:ucanaccess:" + database);
//Call VerifyLabel(<MAC>,<MODEL>); Call provided client
CallableStatement cStmt = conn.prepareCall("{CALL verify(?, ?)}");
//MAC
cStmt.setString(1, "mac address");
//model
cStmt.setString(2, "model");
cStmt.execute();
//Getting results from "Status" column
ResultSet rs1 = cStmt.getResultSet();
//Iterate results and print.
while (rs1.next()) {
System.out.println(rs1.getString("Status"));
}
//Close connection conn
rs1.close();
} catch (SQLException ex) {
Logger.getLogger(CambiumStoredTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CambiumStoredTest.class.getName()).log(Level.SEVERE, null, ex);
}
Using MySql Driver:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:"+ database);
also tried:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+ database);
Error for MySql Driver:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
1) DSN is most commonly assocatiated with ODBC (and often with MS-Access). Hence all the links. ODBC is NOT required for a DSN.
2) Do NOT use Ucanaccess. Use J/Connector for mySQL.
3) Make sure you can communicate with mySQL from the command line. Then focus on getting a small "hello world" JDBC app to connect. Your second and third examples look OK. Be sure to check the mySQL logs for any warnings/errors.
Well, after an entire day of trying to get this to work and sleeping on it for a couple hours I finally got it to work. UCanAccess and mysql-connector did not work. The easiest thing since no other method of connecting to this clients database was acceptable was to push this application in Java 7 rather than 8. This allowed me to Coo=nnect to my DSN with no problems. I understand that this method is not the best solution but it is what is working flawlessly and efficiently. Also, instead of using some rigged up 3rd party libs and jars, I am able to use Connector/J. Thanks everyone for trying to help me. Just incase anyone else runs into this issue, this is how I made it work.
Develope app in Java 7 - not 8.
Set Up System DSN
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//You do not need to provide username or password if it is setup in DSN
Connection conn = DriverManager.getConnection("jdbc:odbc:"+ database);

Cant connect to mariadb from java - only with mysqldeveloper [duplicate]

I am trying to connect to a database in Mariadb through a simple java application but the connection is told to be unsuccessful and an Exception is thrown. I have done the similar connection using mysql and it was working correctly. The problem is maybe with the driver here.
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mariadb://localhost:3306/project", "root", "");
Statement statement = connection.createStatement();
String uname="xyz",pass="abc";
statement.executeUpdate("insert into user values('"+uname+"','"+pass+"')");}//end of try block
I looked up the internet for the help and came by that driver class provided by the MariaDB Client Library for Java Applications is not com.mysql.jdbc.Driver but org.mariadb.jdbc.Driver! I changed it accordingly but it seems the problem is with the very first line inside the try block. The driver is not loading at all.
Also, I have added the mysql jar file to the libraries of my java application as in the screen-shot below. Please help me through this.
It appears that you are trying to use jdbc:mariadb://... to establish a connection to a MariaDB server instance using the MySQL JDBC Driver. That probably won't work because the MySQL JDBC Driver would use jdbc:mysql://..., regardless of whether it is connecting to a MySQL server or a MariaDB server. That is, the connection string must match the driver that is being used (rather than the database server being accessed).
The MySQL and MariaDB drivers are supposed to be somewhat interchangeable, but it only seems prudent to use the MariaDB connector when accessing a MariaDB server. For what it's worth, the combination of mariadb-java-client-1.1.7.jar
and
Connection con = DriverManager.getConnection(
"jdbc:mariadb://localhost/project",
"root",
"whatever");
worked for me. I downloaded the MariaDB Client Library for Java from here:
https://downloads.mariadb.org/client-java/1.1.7/
which I arrived at via
https://downloads.mariadb.org/
Additional notes:
There is no need for a Class.forName() statement in your Java code.
The default configuration for MariaDB under Mageia may include the skip-networking directive in /etc/my.cnf. You will need to remove (or comment out) that directive if you want to connect to the database via JDBC because JDBC connections always look like "network" connections to MySQL/MariaDB, even if they are connections from localhost. (You may need to tweak the bind-address value to something like 0.0.0.0 as well.)
An additional note:
Exploring the MariaDB JDBC driver, I found this inside the url parsing file:
Project: https://github.com/MariaDB/mariadb-connector-j.git
File: src/main/java/org/mariadb/jdbc/UrlParser.java
public static UrlParser parse(final String url, Properties prop) throws SQLException {
....
if (url.startsWith("jdbc:mysql:")) {
UrlParser urlParser = new UrlParser();
parseInternal(urlParser, url, prop);
return urlParser;
} else {
if (url.startsWith("jdbc:mariadb:")) {
UrlParser urlParser = new UrlParser();
parseInternal(urlParser, "jdbc:mysql:" + url.substring(13), prop);
return urlParser;
}
}
As you can see, the string "jdbc:mariadb:" is always replaced with "jdbc:mysql:" internally. So when it comes to the MariaDB driver, whether it is :mariadb: or :mysql: it always gets parsed as "jdbc:mysql:".
No difference.
if (url.startsWith("jdbc:mariadb:")) {
....
parseInternal(urlParser, "jdbc:mysql:" + url.substring(13), prop);
....

Connecting to mySQL Database with Java

In my program I am trying to connect to a mySQL database. This program is written in Java.
I am trying to connect to my database with this code here (? is a place holder b/c I dont know what does there.)
Connection conn = DriverManager.getConnection("jdbc:sqlite:*?*");
I need someone to help me replace the ? to connect to my database. I know the IP (Just call it ***.***.*** for security reasons, the port which is 3306 and the database is called devicede_Test).
Please help me replace the ? with the correct string with the info above, thanks!
Try this
Connection conn = DriverManager.getConnection("jdbc:mysql://***.***.***:3306/dbname");
Try this:
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url,"root", "");
SQLIte is not the right driver here. Use:
Connection conn = DriverManager.getConnection("jdbc:mysql://**.***.***.***/devicede_Test", user, pass);
The JDBC URL format for MySQL Connector/J is as follows, with items in square brackets ([, ]) being optional:
jdbc:mysql://[host][,failoverhost...][:port]/[database] ยป
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
Here is a sample connection URL:
jdbc:mysql://localhost:3306/sakila?profileSQL=true
Source : http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html

How can I connect to a database on my filesystem using Java

I need to connect to a database on my file system but I have noticed that JDBC is referring to a database in memory, any time I want to check the tables need to write a code to retrieve their data, currently I have a SQLite database.
I used the following code but even when I enter a correct address it does not connect to my database.
String sDriver = "org.sqlite.JDBC";
String Database = "users/documents/DB.sqlite";
String sJdbc = "jdbc:sqlite";
Move your database (DB.sqlite) in your working directory. Then use this url to connect to it :
jdbc:sqlite:DB.sqlite
Using JDBC, it will be something like this :
String driver = "org.sqlite.JDBC";
Class.forName(driver);
String dbUrl = "jdbc:sqlite:DB.sqlite";
Connection connection = DriverManager.getConnection(dbUrl);

Does JDBC do anything to protect my password and username from sniffing?

I'm working on a front end for a database I have set up and I was wondering if I just use this code will my password and username show up in plain text if some one is sniffing?
String url = "jdbc:mysql://" + address + "/table";
String user = user_Name;
String password = complete_Password;
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException ex) {
System.out.println(ex);
}
That depends on how the JDBC driver is implemented, the MySQL JDBC driver will not transmit your password in clear text. You can see this happening at the MysqlIO class (look for the changeUser method).
You can also see the various types of authentication MySQL offers (including the very unsafe clear text passwords over the wire) at it's client-server protocol documentation.
I seriously doubt any vendor produced database driver out there will send your password data as clear text over the wire. At least I know the MySQL and PosgreSQL JDBC drivers will not do this. PostgreSQL, for instance, will generate a hash of your password and send it.
JDBC is merely an API. every JDBC driver implementation is different, so this would be up to the particular driver that you were using.

Categories

Resources