• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java application Microsoft SQL server

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Size
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 https://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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot mate for replying.
great help!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 = "jdbc:odbc: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.
 
You may have just won ten million dollars! Or, maybe a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic