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
- Working with the Mysql commands from Java (Netbeans)
- Message box in Java (Netbeans)
- Working with the Java Jtable
- How to connect to database from Netbeans (connection string, Mysql authentication)
- Working with the Java Jpanel
- How ResultSet(RS) works
- User management
- Giving access to users (user permission)
- 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
.
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.
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
).
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.
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