How to Connect to a SQL Server 2008 Database Using Jdbc

Connecting to microsoft sql 2008 r2 from java 8

people are saying that jdk8 removed jdbc driver

That is not true. It was the JDBC-ODBC Bridge that was removed from Java 8. Therefore you cannot use an ODBC driver to connect to SQL Server from Java 8. Instead, you need to use a JDBC driver like this one:

Microsoft JDBC Driver for SQL Server

(The download link in your question is also for Microsoft's JDBC driver for SQL Server. Your problem is that the instructions you are following are for ODBC, not JDBC. Use the instructions for the JDBC driver instead.)

Connecting JDBC with SQL Server 2008

I assume you are not getting any connection

 System.out.println("Enter name:");
Scanner scan=new Scanner(System.in);
String name=scan.nextLine();
System.out.println("Enter age:");
int age=scan.nextInt();
String query="insert into table1(Stuname,Age) values(?,?);";
ps=conn.prepareStatement(query);
ps.setString(1,name);
ps.setInt(2,age);

// you have forget to submit your changes to the database. try with following line of code
ps.execute() or ps.executeQuery()

conn.commit();

JDBC, Connect to sql server

  1. It looks like you're trying to connect to a Microsoft SQLServer using a MySQL driver. You should make sure to use the right driver (http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx). The class is com.microsoft.sqlserver.jdbc.SQLServerDriver. Make sure the driver is in the classpath.

  2. To enable remote connection to the database, you must enable TCP connection on the SQLServer (normally on port 1433). Take a look at this: http://www.scrumdesk.com/Articles/HowToEnableSQLServerRemoteConnections.html

Connect my Sql Server 2008 database to my Java Project with Windows Authentication

The JDBC driver supports the use of Type 2 integrated authentication on Windows operating systems through the integratedSecurity connection string property. To use integrated authentication, copy the sqljdbc_auth.dll file to a directory on the Windows system path on the computer where the JDBC driver is installed.
The sqljdbc_auth.dll files are installed in the following location:

<installation directory>\sqljdbc_<version>\<language>\auth\

For any operating system supported by the Microsoft JDBC Driver for SQL Server, see Using Kerberos Integrated Authentication to Connect to SQL Server for a description of a feature added in Microsoft JDBC Driver 4.0 for SQL Server that allows an application to connect to a database using integrated authentication with Type 4 Kerberos.

Note:
If you are running a 32-bit Java Virtual Machine (JVM), use the sqljdbc_auth.dll file in the x86 folder, even if the operating system is the x64 version. If you are running a 64-bit JVM on a x64 processor, use the sqljdbc_auth.dll file in the x64 folder.
Alternatively you can set the java.libary.path system property to specify the directory of the sqljdbc_auth.dll.

For example, if the JDBC driver is installed in the default directory, you can specify the location of the DLL by using the following virtual machine (VM) argument when the Java application is started:
-Djava.library.path=C:\Microsoft JDBC Driver 4.0 for SQL

Server\sqljdbc_<version>\enu\auth\x86

http://msdn.microsoft.com/en-us/library/ms378428.aspx

Error in connect to SQL Server 2008 R2 using JDBC

I add sqljdbc.jar and import that :

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.*;

I define all need variable in Global ans Static :

private static SQLServerDataSource ds = null;
private static Connection con = null;

and define a method for connect:

    public static boolean getConnection() {
try {
System.out.println(ServerName);
ds = new SQLServerDataSource();
ds.setServerName(ServerName);
ds.setPortNumber(1433);
ds.setDatabaseName(DatabaseName);
ds.setUser(UserName);
ds.setPassword(Password);
con = ds.getConnection();
if (con != null) {
System.out.println("Success");
}
stmt = con.createStatement();
return true;
} // Handle any errors that may have occurred.
catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}


Related Topics



Leave a reply



Submit