Hi everyone.
So I get the code going now the question is:
How to make a menu in Java code?
For example:
1.create table
2.insert into table
3.select fro table
4.delete table...
Which will you choose: ex.1
and creates table
I mean like switch case in C++
import java.sql.*;
public class dbAccess
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database =
"jdbc:odbc:Driver:myDB";
Connection conn = DriverManager.getConnection(database, "", "");
Statement s = conn.createStatement();
// the 1st case: create a table
String tableName = "myTable" + String.valueOf((int)(Math.random() * 1000.0));
String createTable = "CREATE TABLE " + tableName +
" (id Integer, name Text(32))";
s.execute(createTable);
// second case: enter value into table
for(int i=0; i<25; i++)
{
String addRow = "INSERT INTO " + tableName + " VALUES ( " +
String.valueOf((int) (Math.random() * 32767)) + ", 'Text Value " +
String.valueOf(Math.random()) + "')";
s.execute(addRow);
}
// this should be third case:Fetch table
String selTable = "SELECT * FROM " + tableName;
s.execute(selTable);
ResultSet rs = s.getResultSet();
while((rs!=null) && (rs.next()))
{
System.out.println(rs.getString(1) + " : " + rs.getString(2));
}
// this should be the 4: drop the table
String dropTable = "DROP TABLE " + tableName;
s.execute(dropTable);
// close and cleanup
s.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Help anyone???