To do this, the simplest way is to use a JDBC driver :
- Downlaod the integral API from here.
- Put it in the /lib of your Tomcat installation folder if you want to use it in a web project or any folder accessible in your hard disk
- Right click on your project in Eclipse > Properties > Java Build Path > Add External JARs… > Choose the jar : sqljdbc4.jar from the installation folder > OK. the jar will appear in the build path. Click on OK.
- Use the following code in your method :
private static final String connectionString =”jdbc:sqlserver://IP_OR_NAME_SERVER;DatabaseName=DATABASE_NAME;user=USER;password=PWD“;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
conn = DriverManager.getConnection(connectionString);
stmt = conn.createStatement();
String sqlQuery=”YOUR_QUERY_HERE “; // You put here your query : SELCET, UPDATE, DELETE, INSERT
stmt.executeQuery(sqlQuery); // If it is a SELECT QUERY e.g : sqlQuery=”SELECT user,email FROM user”;
while ( rs.next() ){ // To get data from result
Person user = new Person(); user.setName(rs.getString(“user”)); user.setEmail(rs.getString(“email”));
usersList.add(user);
}
stmt.executeUpdate(sqlQuery); // If it is an UPDATE, DELETE or INSERT QUERY
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (rs != null)
try { rs.close(); } catch (Exception e) { // LOGGER }
if (stmt != null)
try { stmt.close(); } catch (Exception e) { // LOGGER}
if (conn != null)
try { conn.close(); } catch (Exception e) {// LOGGER }
} - Congratulation… You made a connection with your sqlServer database by using JAVA and JDBC.
See also :
- How to install a new tomcat server in your workspace?
- How to make a connection with a mySql database by using Java?
- How to make a web service by using Eclipse?
- How to create a web service SOAP?
5 comments
No ping yet
Boite de com says:
September 23, 2011 at 12:29 am (UTC 0)
I’ve recently been searching for precisely this info. While I used to be expecting to locate…
Low Fat Foods says:
September 16, 2011 at 8:41 am (UTC 0)
Hello! I realize this is kind of off-topic however I needed to ask. Does operating a well-established website like yours require a massive amount work? I am completely new to blogging but I do write in my diary every day. I’d like to start a blog so I will be able to share my experience and feelings online. Please let me know if you have any kind of suggestions or tips for new aspiring blog owners. Appreciate it!
jalal mjadli says:
September 16, 2011 at 10:52 pm (UTC 0)
Hello! I see what you want to say, but to do this quickly:
To have a good blog (layout + content): yes it requires a massive amount work : you start by finding the idea of your blog + to choose a good layout + to write
To find idea : I think you should focus on your experience and your knowledge.
I’m thinking seriously to write a post sharing my experience in blogging even it still young and modest…
Forex trading systems says:
September 4, 2011 at 6:45 am (UTC 0)
Appreciation pro this article. Present are categorically tips participating in at this juncture to I choice benefit.
car repo says:
August 9, 2011 at 9:44 am (UTC 0)
Hey blogger, nice work with the choice of theme on this blog. It looks reall nice.