java 判断数据库表是否存在,不存在则创建。

package test;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class test {

	 public static String url =
	 "jdbc:mysql://192.168.1.102:3306/*****_orders";
	 public static String user = "*****";
	 public static String password = "123456";
	public static void main(String[] args) throws ClassNotFoundException,
			IOException, SQLException, ParseException {

		 String createBrandDatabase = "create table "
		 + "userbrandtime"
		 +
		 "(uid int(10) unsigned NOT NULL,brand_ids int(10) unsigned NOT NULL,timestamp int(10) unsigned NOT NULL,primary key(uid,brand_ids))"
		 + " DEFAULT CHARSET=utf8";
		 String checkTable="show tables like \"userbrandtime\"";
		 try {
		 Class.forName("com.mysql.jdbc.Driver");
		 Connection con = (Connection) DriverManager.getConnection(url,
		 user, password);
		 Statement stmt = (Statement) con.createStatement();
		 
		 ResultSet resultSet=stmt.executeQuery(checkTable);
		 if (resultSet.next()) {
			System.out.println("table exist!");
		}else{
                 if(stmt.executeUpdate(createBrandDatabase)==0)
		 System.out.println("create table success!");
		}
	
		 } finally {
		
		 }

	}
}


你可能感兴趣的:(java)