3

I'm pretty new with Java (though I'm experienced with C#). Anyway I've Googled a lot in the last few hours and I can't understand something. I'm hoping you can help me with some guidance.

I'd like to use Eclipse with SQL Server and I have no idea how am I doing that. I've seen some plugin called SQLExplorer but I just can't figure what are the steps to integrate with that.

Regarding:

  • What is the best way of doing that?
  • Is there any guide or full tutorial?
  • Maybe you have some source code example of a connection to SQL?
  • Should I use that plugin? How?
  • Why is it so difficult to find info about this issue? Isn't eclipse familiar with SQL?

Thank you all.

1 Answer 1

6

Java and databases go together like bread and butter, but the language is just different enough that you might have issues breaking in. There are a number of different levels at which things can be integrated: the traditional query/result API is called JDBC, and all you need to work with any database from Java code is the appropriate JDBC driver. Here is the official one from Microsoft for SQL Server, and here is a tutorial about using the JDBC API.

Above that, there are object-relational mapping tools like Hibernate which let you persist Java objects directly onto your database. Hibernate automates a lot of the mapping and lets you work at a high level. Hibernate is an enormous subject; start here.

What SQLExplorer, and tools like it, let you do is browse around in a database, exploring the tables and the data in them. It's not something you use with code, but rather interactively to examine the data you're working with.

Here is a JDBC "Hello World," assuming the default database on the local machine, a table named some_table_name has a character-valued column in the first position:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost";
Connection con = DriverManager.getConnection(connectionUrl);
Statement s = con.createStatement();
ResultSet r = s.executeQuery("SELECT * FROM some_table_name");
while (r.next()) {
    System.out.println(r.getString(1));
}
Sign up to request clarification or add additional context in comments.

12 Comments

Erm, i installed that driver, checking out that tutorial and also viewed some ytube videos, i cant find something relevant to SQL Server (only for apache/mysql), can u give me an extra guide?
It's exactly the same, except the connection information (i.e., the name of the class, and the connection string) are appropriate for the driver. See, for example, msdn.microsoft.com/en-us/library/ms378428(v=SQL.90).aspx and msdn.microsoft.com/en-US/library/ms378956(v=sql.90).aspx (under "Creating a connection with the DriverManager class).
i was viewing this in the past hour and also tried to work with hibernate, im guessing im doing something wrong but can't figure what.. :-(
I added a "Hello, World" program for you to try. What sort of errors are you seeing?
im trying to run ur code (as i imported import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; ) but the code doesnt compile, this line: "while (r.hasNext()) { " The method hasNext() is undefined for the type ResultSet
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.