GeeCON Prague 2014*
The moose likes JDBC and the fly likes Java application Microsoft SQL server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Java application Microsoft SQL server" Watch "Java application Microsoft SQL server" New topic
Author

Java application Microsoft SQL server

Jim Size
Greenhorn

Joined: Aug 10, 2010
Posts: 29
hello guys, i have a problem with a project in DBMS.
i dont want too much help i just want some suggestions.

in my project i need to make a database and do some work on "views" , "indexes" etc. (everything is perfect at the moment)

but i need to make my database somehow work like an "application". So i searched the internet and find out that i can make it (as they say) through JDBC.

i know some stuff for java but i do not know how i can make my database work like an independent application (probably jar).

it will be great if you suggest me some links or what should i read-learn so i can make it work.
also, do i need to write again all my DB in netbeans for example?? (because i have made it in Microsoft SQL server)

thank you for your time.

i use bluej-eclipse-netbeans for java and
Microsoft SQL server 2008 - netbeans for db
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
NetBeans is an IDE - it has no impact on the decision of which DB to use.

JDBC is the Java database access API - you'd use it no matter which DB was being used underneath.

What, exactly, do you mean by "make my database work like an independent application (probably jar)"? Do you want a standalone solution that doesn't make use of a DB server? If so, check out the HSQLDB and Derby databases - both are written in Java and can be embedded in Java applications. If you meant something else, please provide more detail.


Ping & DNS - my free Android networking tools app
Jim Size
Greenhorn

Joined: Aug 10, 2010
Posts: 29
Maybe i didn't expressed my problem very well, you are right.

well my goal is to make an application for "users" who don't know how to use SQL and want to get some results from the database that i have already created in MSQL server.

So i decided to make a jar file.

My .jar program will have the GUI (buttons, menu etc) where the user can insert information into the table fields etc (simple stuff).
*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_
i have already made my DB - i have made all my tables etc etc in MSQL server.

GOAL :

GUI in java where user can acquire information from my db.
and i asked if you can tell me how can i connect this java program with my already made DB

for example somewhere of course in the code i will have something like this:


if(e.getSource()==buttonInsert){
....
}
and some "connectToSql" in the main method

i don't want the solution as i said before i want to read probably from some links etc and learn from it.

i hope i made my self clear

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42035
    
  64
The piece you need is the JDBC API I mentioned: http://download.oracle.com/javase/tutorial/jdbc/basics/

You'll need to get a JDBC driver for SQL Server. I'd start with Microsoft's own driver, which is linked on http://www.coderanch.com/how-to/java/GeneralJdbcQuestions. There's information on how to use it on the various MSDN pages linked from its download page.
Jim Size
Greenhorn

Joined: Aug 10, 2010
Posts: 29
thanks a lot mate for replying.
great help!
Shahriar Khondokar
Greenhorn

Joined: Jun 11, 2010
Posts: 2
To connect to SQL Server from a Java application, you need to use the JDBC API. The JDBC API provides classes and methods that connect to the database, load the appropriate driver, send SQL queries, retrieve results etc.

HOW TO CONNECT TO THE DATABASE
A ‘Connection’ object represents a connection with a database.
To establish the connection, use the method ‘DriverManager.getConnection’. This method takes a string containing a URL which represents the database we are trying to connect to.
Below is the sample code for establishing a connection:
private String DATABASE_URL = "jdbcdbc:embedded_sql_app";
// establish connection to database
Connection connection = DriverManager.getConnection( DATABASE_URL,"sa","123" );
Detailed discussion about the Database URL and how to create it can be found in the resource provided at the end of this post.

QUERYING THE DATABASE
The JDBC API provides three interfaces for sending SQL statements to the database, and corresponding methods in the ‘Connection’ interface create instances of them.
1. Statement - created by the ‘Connection.createStatement’ methods. A ‘Statement’ object is used for sending SQL statements with no parameters.
2. PreparedStatement - created by the ‘Connection.prepareStatement methods’. A ‘PreparedStatement’ object is used for precompiled SQL statements. These can take one or more parameters as input arguments (IN parameters).
3. CallableStatement - created by the ‘Connection.prepareCall’ methods. ‘CallableStatement’ objects are used to execute SQL stored procedures from Java database applications.

RETRIEVING THE RESULT
A ‘ResultSet ‘is a Java object that contains the results of executing a SQL query. The data stored in a ‘ResultSet’ object is retrieved through a set of get methods that allows access to the various columns of the current row. The ‘ResultSet.next’ method is used to move to the next row of the ‘ResultSet’, making it the current row.
The following code fragment executes a query that returns a collection of rows, with column ‘a’ as an ‘int’, column ‘b’ as a ‘String’, and column ‘c’ as a ‘float’:
java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
// retrieve and print the values for the current row
int i = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
System.out.println("ROW = " + i + " " + s + " " + f);
}

This is just a brief introduction on how to interact with a database from Java. For more details on the items discussed above as well as information on passing parameters, executing stored procedures etc. please refer to the following resource: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html#Shahriar_N_Embedding_SQL_in_Java )
Here, you will also find information on how to interact with a database programmatically; i.e. without using SQL.
Hope you find this useful.
 
GeeCON Prague 2014
 
subject: Java application Microsoft SQL server