How can I create a MySQL table with specific column names and types using Java?

  • Context: Java 
  • Thread starter Thread starter Franco
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around creating a MySQL table using Java, focusing on how to dynamically construct SQL statements with specific column names and types provided as arrays. Additionally, participants seek guidance on retrieving data from a MySQL database and displaying it in a Swing GUI.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks help in constructing the SQL statement for creating a table, expressing uncertainty about using arrays for column names and types.
  • Another participant attempts to create a SQL string but acknowledges that their initial approach is incorrect and is unsure how to properly format it.
  • A later post suggests using a for-loop to iterate over the column names and types to build the SQL string, but does not confirm the correctness of this approach.
  • One participant requests assistance on how to retrieve string data from a MySQL database and display it in a Swing GUI JDialog.
  • Another participant shares a partial implementation of a Swing GUI but expresses uncertainty about fetching a username from the MySQL database.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the correct method for constructing the SQL statement or on the best way to retrieve and display data in the GUI. Multiple competing views and uncertainties remain.

Contextual Notes

Participants have not resolved specific assumptions about the structure of the SQL statement or the methods for database interaction, and there are unresolved details regarding the Swing GUI implementation.

Who May Find This Useful

This discussion may be useful for Java developers working with MySQL who are looking for guidance on dynamic SQL creation and integrating database interactions with Swing applications.

Franco
Messages
12
Reaction score
0
need help with some coding


public void createTable(String tableName, String[] columnNames, String[] columnType, String[] Keys) throws SQLException {

try {
stmt = conn.createStatement();
String sql = <need help here>
stmt.executeUpdate(sql);
}
catch (SQLException e) {
}
}

not sure how to type the code for String sql
with columnNames, columnType as arrays...
should i actually for-loop it??
 
Technology news on Phys.org
so far.. i got

String sql = "CREATE TABLE " + tableName + "(" + columnNames + " VARCHAR(32))";

i know it's wrong , but then not sure how i should write it...
another word... stucked :)
 
Franco said:
not sure how to type the code for String sql
with columnNames, columnType as arrays...
should i actually for-loop it??

PHP:
String sql = "CREATE TABLE " + tableName + " (";
for (int i = 0; i < columnNames.length; i++) {
    sql += columnNames[i] + " " + columnType[i] + ",";
}
sql += ");";
 
thx wave ^_^
and can someone actually teach me..

how to retrieve a string data from MySQL database, and display it in a swing GUI JDialog?? pop-up

and a swing GUI that displays some particular information from MySQL database (in table)
 
so far i got something like...
PHP:
import java.awt.*;
import javax.swing.*;

public class MainGUI extends JFrame {
JDialog welcome Window;

public MainGUI() {
super();
// unnecessary codes, no problem here

}

public void initComponents() {
this.welcomeWindow = new JDialog();
welcomeWindow.showMessageDialog(null, "Welcome " + username);

}

}

well my codes something like this rite now

not too sure... if i want to fetch username from the MySQL database

any clue? anyone can help me a bit? :D
 

Similar threads

Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K