I Am Using Eclips Ide.
- After Create A Project First Download Jdts.jar Library. link
- Second Create A libs Folder And Past library File under libs folder .
- From Project->Propities->JavaBuildpath->Library And Add Jar File Now Jar File Is Ready For Work
- Follow The Code
package sap;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SapLogin {
// Your IP address must be static otherwise this will not work. You //can get your Ip address
//From <em>Network and security in Windows.</em>
String ip = "192.168.10.101";
// This is default if you are using JTDS driver.
String classs = "net.sourceforge.jtds.jdbc.Driver";
// Name Of your database.
String db = "NEWFIZTEST";
// Userame and password are required for security.
// so Go to sql server and add username and password for your database.
String un = "sa";
String password = "fizsa7,";
public Connection CONN() {
Connection conn = null;
String ConnURL;
try {
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
}
catch (SQLException se)
{
}
catch (ClassNotFoundException e) {
}
catch (Exception e) {
}
return conn;
}
public static void main(String[] args) {
SapLogin sapLogin = new SapLogin();
Connection connection = sapLogin.CONN();
if (connection!=null) {
System.out.println("connected");
}
}
}
Connect With Mysql Server Code
package sap;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MysqlConnect {
// Your IP address must be static otherwise this will not work. You //can get your Ip address
//From <em>Network and security in Windows.</em>
String ip = "192.168.10.44";
// This is default if you are using JTDS driver.
String classs = "com.mysql.jdbc.Driver";
// Name Of your database.
String db = "NEWFIZTEST";
// Userame and password are required for security.
// so Go to sql server and add username and password for your database.
String un = "root";
String password = "usbw";
public Connection conn() {
Connection conn = null;
String ConnURL;
try {
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
}
catch (SQLException se)
{
}
catch (ClassNotFoundException e) {
}
catch (Exception e) {
}
return conn;
}
public static void main(String[] args) {
SapLogin sapLogin = new SapLogin();
Connection connection = sapLogin.CONN();
if (connection!=null) {
System.out.println("connected");
}
}
}
One thought on “Java Connect With Sql Server,mysql server Using JDTS JDBC DRIVER and mysql-connector-java-8.0.17 driver”