简体   繁体   English

使用 MySQL 表的 java jdbc 更改数据类型

[英]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?如何使用与 MySQL 中表的 Java 的 JDBC 连接将数据类型从varchar更改为int

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.我使用导入选项导入的文件在 varchar 中有值,但我只需要该表中的整数值。 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:当我在 phpmyadmin 中使用 alter 查询时,它工作得很好,但是当我尝试在 jdbc 中进行更改时,它给了我错误:

Incorrect integer value: at 'col 1';

jdbc code was as: jdbc 代码如下:

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.这实际上是一个 pdf 文件并将其转换为 .csv 并导入到我的数据库中 我只需要表中的整数值而不需要任何其他字母和符号。 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?此代码仅用于一列,我需要更改所有三列。那么我应该怎么做才能使用 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 :就像@Mark Rotteveel 在评论中所说的那样,你的 Query 有问题,所以你的列名应该在两个``而不是两个''""之间,如果你的列名包含不止一个部分,这将不起作用更改您应该使用的列的类型:

//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如果您更改类型,这将更改所有列,并将其设置为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.我建议避免使用两个部分的名称,您可以使用 col_name 代替,因为这会导致问题,例如,如果您使用 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 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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