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

  • Thread starter Thread starter Franco
  • Start date Start date
Click For Summary
The discussion revolves around coding assistance for creating a SQL table and retrieving data from a MySQL database using Java. The user is trying to construct a SQL statement within a method to create a table using arrays for column names and types. They initially attempted to concatenate the arrays directly into the SQL string but realized it was incorrect. A suggested solution involves using a for-loop to iterate through the column names and types, appending them to the SQL string correctly. Additionally, the user seeks guidance on how to retrieve a string from a MySQL database and display it in a Swing GUI JDialog. They provided a basic structure for a Swing application but expressed uncertainty about fetching the username from the database. The conversation highlights the need for clearer coding practices and effective database interaction within Java 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
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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