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
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
Back
Top