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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top