i wish to add another column using code in JDBC.I have one column columnname i wish to add a text type(data type in access) column in the access file
my code is

import java.sql.*;
    public class Test3
    {
    public static void main(String[] args)
    {
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    /* the next 3 lines are Step 2 method 2 from above - you could use the direct
    access method (Step 2 method 1) istead if you wanted */
    String dataSourceName = "mdbTest";
    String dbURL = "jdbc:odbc:" + dataSourceName;
    Connection con = DriverManager.getConnection(dbURL, "",""); 
    // try and create a java.sql.Statement so we can run queries
    Statement s = con.createStatement();
    s.execute("create table TEST12345 ( column_name integer)"); // create a table
    
    s.close(); // close the Statement to let the database know we're done with it
    con.close(); // close the Connection to let the database know we're done with it
    }
    catch (Exception err) {
    System.out.println("ERROR: " + err);
    }
    }
    }

i have set the drivers using the following code

class Test3
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println("error:" +e);
}
}
}

i have also created a blank database through ODBC with datasource name as mdbTest.

I wish to create another column. And then insert values into them. I have a rough idea about inserting. But i wish to add another column of different data type in the first place.How do i modify the code?

Recommended Answers

All 4 Replies

i wish to add another column using code in JDBC.I have one column columnname i wish to add a text type(data type in access) column in the access file
my code is

import java.sql.*;
    public class Test3
    {
    public static void main(String[] args)
    {
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    /* the next 3 lines are Step 2 method 2 from above - you could use the direct
    access method (Step 2 method 1) istead if you wanted */
    String dataSourceName = "mdbTest";
    String dbURL = "jdbc:odbc:" + dataSourceName;
    Connection con = DriverManager.getConnection(dbURL, "",""); 
    // try and create a java.sql.Statement so we can run queries
    Statement s = con.createStatement();
    s.execute("create table TEST12345 ( column_name integer)"); // create a table
    
    s.close(); // close the Statement to let the database know we're done with it
    con.close(); // close the Connection to let the database know we're done with it
    }
    catch (Exception err) {
    System.out.println("ERROR: " + err);
    }
    }
    }

i have set the drivers using the following code

class Test3
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println("error:" +e);
}
}
}

i have also created a blank database through ODBC with datasource name as mdbTest.

I wish to create another column. And then insert values into them. I have a rough idea about inserting. But i wish to add another column of different data type in the first place.How do i modify the code?

Do you want to add the column programatically?
If YES then you can invoke executeUpdate() method and alter the structure of the table.

String table="columnname ";
String dataType="integer";
s.executeUpdate("ALTER TABLE "+table+" ADD "+col+" "+type);

ok thanks for that. I want to create more than one columns of different data types while creating the table.How do i manipulate that?(In the code where i write the sql statement for creating the table

I've already given you the code snippet. You can create as many columns as you want.
Just invoke executeUpdate() method on the Statement reference that you get.
Hope I've made myself clear. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.