Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / MySQL

Message Manager

5.00/5 (4 votes)
8 Feb 2012CPOL1 min read 25.5K   1.1K  
Sending and receiving message, user authentication, user permission, priority message
Image 1

Introduction

As you know, the amount of source code for Java (Netbeans) on the Internet is much less than other languages??. So I sent this article. This article may be helpful for people who work with Netbeans, Java(J2SE), Mysql. In this article, you will learn: Sending and receiving message, user authentication, user permission, priority message.

Training Tips Article

  1. Working with the Mysql commands from Java (Netbeans)
  2. Message box in Java (Netbeans)
  3. Working with the Java Jtable
  4. How to connect to database from Netbeans (connection string, Mysql authentication)
  5. Working with the Java Jpanel
  6. How ResultSet(RS) works
  7. User management
  8. Giving access to users (user permission)
  9. Priority for letters

In the first, manager with username: admin and password: 313 is entered and the user defines and gives to each user the appropriate permissions.

Using the Code

The database connection and functions are in Database.java. The following functions and variables are used in this class.

Connection String: A connection string contains initialization information that is passed as a parameter from a data provider to a data source. The syntax depends on the data provider, and the connection string is parsed during the attempt to open a connection. Here we call that connectionUrl.

C#
//connection string //"Archive" is database name

private static String connectionUrl = 
    "jdbc:mysql://localhost:3306/archive user=root & password=123"; 

Retrieve Data function: The function to retrieve records from database and fill those in result set, this function has an input.

C#
public static void retrieveData(String SQL) throws Exception {
    rs = stmt.executeQuery(SQL);
    while (rs.next()) {
        System.out.println(rs.getString("ProductName") + " : " + 
    rs.getString("QuantityPerUnit") + " : " +
        rs.getString("UnitPrice") + " : " + 
    rs.getString("UnitsInStock") + " : " +
        rs.getString("UnitsOnOrder") + " : " + 
    rs.getString("ReOrderLevel") + " : " + rs.getString("Discontinued"));
    }
}

Command function: The function for send commands to the database (insert, update, delete).

C#
public static void Command(String SQL) throws Exception {
    try  {
        Class.forName("com.mysql.jdbc.Driver");
        con=  DriverManager.getConnection(connectionUrl);
        stmt = con.createStatement();
        stmt.executeUpdate(SQL);
            System.out.print("?? ?????? ????? ??.");
        }catch(Exception insError){
            System.out.print(insError);            }
}

Select function: This function is used for selecting record(s) with any condition from database and filling those in result set.

C#
public ResultSet select(String SQL)  {
    try  {
    Class.forName("com.mysql.jdbc.Driver");
    con=  DriverManager.getConnection(connectionUrl);
    stmt = con.createStatement();
     ResultSet s=stmt.executeQuery(SQL);
        return s;
    }catch(Exception insError){
        System.out.print(insError);
    }
    return null;
}

Points of Interest

Netbeans is not just for mobile programming. We can develop in this IDE web, Java desktop, Enterprise, PHP, C/C++, etc. application.

History

  • 7th February, 2012: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)