Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 21st, 2005, 08:44 AM
Friend of Wrox
 
Join Date: Mar 2005
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to create a GUI that connect to sql server

Hi guys i wonder how i can create a simple java gui that connect to sql sever db and display a table records. I be be happy if some one show me some code. thanks

 
Old April 15th, 2005, 07:22 AM
Registered User
 
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You probably already found a solution, but you can download a jar file containing the SQL driver here: http://jtds.sourceforge.net/

Here is some source code that uses that database driver to obtain a basic connection to an SQLServer. This particular connection allows you to scroll around in a resultset:

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectToDBScroll
{
    private Connection conn=null;
    public ConnectToDBScroll(){
        try{conn = getConnection();
        }catch (SQLException ex)
        {
            while (ex != null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        }catch (IOException ex)
        {
            ex.printStackTrace();
        }
        }
    public void closeConnection(){
        try{
            conn.close();
            System.out.println("Connection Closed");
        }catch (SQLException ex){
            while (ex != null)
            {ex.printStackTrace();
                ex = ex.getNextException();
            }
        }
    }
    public ResultSet runQuery(String theQuery){
        ResultSet rs = null;
        try
        {
            Statement stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSI TIVE,ResultSet.CONCUR_READ_ONLY);
            rs = stat.executeQuery(theQuery);
        }
        catch (SQLException ex)
        {
            while (ex != null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        }
        return rs;
    }
    // This is used to update a Table with a new row.
    public boolean makeUpdate(String theQuery)
    {
        boolean check = false;
        try
        {
            Connection conn = getConnection();
            Statement stat = conn.createStatement();
            int x = stat.executeUpdate(theQuery);
            if(x > 0)
                check = true;
            conn.close();
        }
        catch (SQLException ex)
        {
            while (ex != null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    return check;
    }

 /* Gets a connection from the properties specified
    in the file database.properties and returns the
    database connection
 */

//this is for mysql, sqlserver below
 //jdbc.drivers=com.mysql.jdbc.Driver
 //jdbc.url=jdbc:mysql://servername.domain.prefix:port/databasename
 //jdbc.username=yourusername
 //jdbc.password=yourpassword

    public static Connection getConnection()
    throws SQLException, IOException
    {
        try{
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            }
            catch (Exception ex) {
                System.out.println("failed");
            }
    //jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
        String url = "jdbc:jtds:sqlserver://server:port/database;User=username;Password=password";
        Connection conn = DriverManager.getConnection(url);
        return DriverManager.getConnection(url);
    }
}

As for the gui part, you have to build that yourself. There is probably a Java API that does this for you. The easiest thing to do is to use an Object[][] to hold the Resultset data. You can use that Object[][] to build a TableModel, and then use that TableModel to build a JTable. You can then use the JTable in a GUI. You could also find an API that does this for you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
connect sql-server with c# technorakesh C# 1 April 22nd, 2006 08:09 AM
Cannot Connect to SQL Server JoshCBR28 BOOK: Beginning ASP 3.0 2 October 25th, 2005 10:27 AM
Cannot connect to SQL server tsimsha SQL Server ASP 1 July 22nd, 2005 07:21 AM
How to connect to SQL SERVER Wizard VB Databases Basics 1 February 5th, 2005 11:29 AM
[000] Unable to connect to server 'SERVER\SQL'; SQ ctanchan SQL Server 2000 1 August 15th, 2003 07:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.