Results 1 to 4 of 4
  1. #1
    sumpit is offline Member
    Join Date
    Jan 2015
    Posts
    1
    Rep Power
    0

    Question how to create a new java class for database connection ?

    Hi all,
    Newbie here, please go easy on me :)
    I'm an entrepreneur. I have a shop and I use visual studio.net to create an Inventory application to manage my shop. So I am familiar with vb.net codes.
    Now, I'm trying to built another Inventory application for my shop using java, 'coz now I'm using Linux Ubuntu for my OS desktop and there's no visual studio.net in linux.

    So in vb.net, there's a module that I use for database connection that I can call from any form that I needed.
    I searched google for the equivalent module for java and there is none. But, there's an example that use a new java class.

    Here's the code in MySQLConnect.java :
    Java Code:
    public class MySQLConnect {
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
        static final String DB_URL = "jdbc:mysql://localhost/dbInventory";
    
        static final String USER = "root";
        static final String PASS = "root";
        
        public static Connection ConnectDb() {
            
            try {
                Class.forName(JDBC_DRIVER);
                Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
                
                return conn;
                
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
        
    }
    Here's the code in frItems.java :
    tbItems is jTable.

    Java Code:
    public class frItems extends javax.swing.JInternalFrame {
        public frItems() {
            initComponents();
            
            Connection conn = null;
            Statement stmt = null;
            String sql = null;
                            
            try{
                conn = MySQLConnect.ConnectDb();
    
                stmt = conn.createStatement();
                sql = "SELECT * FROM mItems";
                rs = stmt.executeQuery(sql);
                
                DefaultTableModel tbl = new DefaultTableModel();
                tbl.addColumn("Item_Code");
                tbl.addColumn("Item_Name");
                tbl.addColumn("Item_Desc");
            
                tbItems.setModel(tbl);
          
                while(rs.next()){
                    tbl.addRow(new Object[]{
                        rs.getString("Item_Code"),
                        rs.getString("Item_Name"),
                        rs.getString("Item_Desc")
                    });
                    tbItems.setModel(tbl);
                    
                }
                
                rs.close();
                stmt.close();
                conn.close();
            }catch(SQLException se){
                //Handle errors for JDBC
                se.printStackTrace();
            }catch(Exception e){
                //Handle errors for Class.forName
                e.printStackTrace();
            }finally{
                //finally block used to close resources
                try{
                    if(stmt!=null)
                        stmt.close();
                }catch(SQLException se2){
                }// nothing we can do
                try{
                    if(conn!=null)
                        conn.close();
                }catch(SQLException se){
                    se.printStackTrace();
                }//end finally try
            }//end try
        }
    It didn't work.

    Please help me. Where did I go wrong there ?

    Thank you very much for the help..

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: how to create a new java class for database connection ?

    What exactly does "it didn't work" mean?
    Did you get an exception?
    Did you get incorrect results?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how to create a new java class for database connection ?

    coz now I'm using Linux Ubuntu for my OS desktop and there's no visual studio.net in linux.
    About Mono | Mono

    C# and .NET's Sudden Ubiquity | Dr Dobb's
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: how to create a new java class for database connection ?

    did you import the jdbc classes and did you install the jdbc driver for your db?

Similar Threads

  1. Create database connection which accessible for all users
    By mohsen.noor in forum Java Servlet
    Replies: 2
    Last Post: 11-24-2014, 01:52 PM
  2. Java 8 Database Connection Pool Library
    By johnmerlino in forum New To Java
    Replies: 4
    Last Post: 05-31-2014, 09:13 PM
  3. database connection with java application
    By FaisalShahzad in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2013, 11:55 AM
  4. database connection with java application
    By FaisalShahzad in forum New To Java
    Replies: 1
    Last Post: 12-09-2013, 11:51 AM
  5. cant create database connection
    By gvjoshi25 in forum NetBeans
    Replies: 3
    Last Post: 08-19-2011, 11:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •