简体   繁体   中英

Change datatype using java jdbc of MySQL table

How to change the datatype from varchar to int using JDBC connection with Java of a table in MySQL?

I tried to use the query:

Alter table table_name CHANGE 'col 1' 'col 1' int (5 );

The file I am importing using the import option has the values in varchar, but I only required integer values from that table. The values of the imported table are not in ordered but the integer values are useful for me.

When I used the alter query in the phpmyadmin it works absolutely fine, but when I tried to alter in jdbc it gives me the error:

Incorrect integer value: at 'col 1';

jdbc code was as:

try {
    Connection co=DriverManager.getConnection("jdbc:mysql://localhost:3306/csv_db","root",null);
    String c= " ALTER TABLE tpf1 MODIFY `col 3` int(5)";  
    String t="Alter table table_name CHANGE 'col 1' 'col 1' int (5 );";
    Statement stmt=co.createStatement();

    stmt.executeUpdate(t);

} catch (SQLException ex) {
    JOptionPane.showMessageDialog(this, ex);
}

Edit

try {
    Connection co=DriverManager.getConnection("jdbc:mysql://localhost:3306/csv_db","root",null);
    String c= " ALTER TABLE tpf1 MODIFY `col ` int(20)";  

    Statement stmt=co.createStatement();

    stmt.executeUpdate(t);

} catch (SQLException ex) {
    JOptionPane.showMessageDialog(this, ex);
}

The table screenshot is in the Photo: 表

This is actually a pdf file and converted it into .csv and imported to my database And i only need integer values from the table not any other alphabets and symbol. This code is only for one column and i need to alter all the three columns.So what should i do to get only integer values using jdbc? :| :)

Like @Mark Rotteveel said in comment, you have a problem in your Query so the name of your columns should be between two `` and not between two '' or "" this not work if your column name contain more then one part so to change the type of your column you should to use this :

//change column type
String query = "ALTER TABLE tablename MODIFY `col 1` INT(5)";
Statement stmt = co.createStatement();
int rslt = stmt.executeUpdate(query);

To change the name or your column use this :

//change column name
query = "ALTER TABLE tablename CHANGE `old name` `new name` INT(5);";
stmt = co.createStatement();
rslt = stmt.executeUpdate(query);

so you can check if your query executed successfully like this :

if (rslt > 0) {
    System.out.println("Success");
} else {
    System.out.println("Failed");
}

Note

If you change the type this will change all the column, and set it to 0

I suggest to avoid two use a name with two parts you can use col_name instead because this make a problem for example if you are using JPA.

EDIT

I try this piece of code and it work fine :

public class UpdateColumn {

    static String driver = "com.mysql.jdbc.Driver";
    static String DB_username = "root";
    static String DB_password = "password";
    static String DB_URL = "jdbc:mysql://localhost:3306/db_name";

    public static void main(String args[]) {
        /*read from the file*/
        try {
            Class.forName(driver);
            java.sql.Connection co = DriverManager.getConnection(DB_URL, DB_username, DB_password);
            //change column type
            String query = "ALTER TABLE tablename MODIFY `c 1` INT(5);";
            Statement stmt = co.createStatement();
            int rslt = stmt.executeUpdate(query);
            System.out.println(rslt);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

MySQL

create database db_name;

use db_name;

drop table t1;

create table tablename(
`c 1` varchar(5),
`c 2` int
);

INSERT INTO tablename values('java', 1);
INSERT INTO tablename values('mysql', 2);

Good luck.

statement.executeUpdate("ALTER TABLE table_name ALTER COLUMN column_name datatype");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM