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
AI Thread 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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top