Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Creating a MySQL Database Table to store Java Types

Dear user, consider a case where we need to store a java types in our database table. Now one question may arise in your mind that whether the MySQL supports java types or not.

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table Change Column Name of a Table Adding a New Column Name in Database Table Retrieving All Rows from a Database Table Inserting values in MySQL database table Retrieving Tables from a Database Deleting a Table from Database Description of Database Table Creating a MySQL Database Table to store Java Types Creating a Database Table Creating a Database in MySQL
Ads

Creating a MySQL Database Table to store Java Types

     

Dear user, consider a case where we need to store a java types in our database table. Now one question may arise in your mind that whether the MySQL supports java types or not. This section describes how to create a table in MySQL database that stores all java types. Here we are providing you an example with code for creating a table to store java types. Brief description is given below:

Description of program:

In this program we are going to establish the connection between MySQL database table and java file. After the connection has been established creates a database table for storing all java types. We have used most of the java types provided to us by the jdbc. 

Here is the code of program:

import java.sql.*;

public class CreateMySqlTable{
  
  public static void main(String[] args) {
  System.out.println("Creating a Mysql Table to Store Java Types!");
  Connection con = null;
  String url = "jdbc:mysql://localhost:3306/";
  String db = "jdbctutorial";
  String driver = "com.mysql.jdbc.Driver";
  String user = "root";
  String pass = "root";
  try{
  Class.forName(driver).newInstance();
  con = DriverManager.getConnection(url+db, user, pass);
  try{
  Statement st = con.createStatement();
<<<<<<< CreateMySqlTable.shtml    String table = 
"CREATE TABLE java_DataTypes2

("
"typ_boolean BOOL, "  
=======   String table 
"CREATE TABLE java_DataTypes2(""typ_boolean BOOL, "  
>>>>>>> 1.7   "typ_byte  TINYINT, "  
  "typ_short SMALLINT, "  
  "typ_int INTEGER, " 
  "typ_long  BIGINT, "  
  "typ_float FLOAT, " 
  "typ_double  DOUBLE PRECISION, "  
  "typ_bigdecimal  DECIMAL(13,0), "  
  "typ_string  VARCHAR(254), "  
  "typ_date  DATE, "  
  "typ_time  TIME, "  
  "typ_timestamp TIMESTAMP, " 
  "typ_asciistream TEXT, "  
  "typ_binarystream  LONGBLOB, "  
  "typ_blob  BLOB)"
 

 st.executeUpdate(table);
  System.out.println(table);
 
con.close
();
 }
  catch (SQLException s){
  System.out.println
(
"Table is all ready exists!");
 }
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
}

Download this example.

Advertisements

Online Training Classes

Share on Google+Share on Google+

Creating a MySQL Database Table to store Java Types

Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Creating a MySQL Database Table to store Java Types   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
javier
May 30, 2011
INSERCION DE DATOS

Buenas fijense que pues intento insertar mas datos a mi base de datos y pues solo me dice que estoy violando la llave primaria y que la estoy duplicando alguien me puede decir que hacer este es el codigo que utilizo String conexion = "jdbc:mysql://localhost/cierre?" + "User= root & Password ="; Connection conecta; try{ conecta = (Connection)DriverManager.getConnection(conexion,"root","Francisco"); String in ="CREATE TABLE IF NOT EXISTS cliente(id int, " + "Nombre varchar(30), Apellido varchar(30), PRIMARY KEY (id));"; Statement st= null; st = (Statement)conecta.createStatement(); JOptionPane.showMessageDialog(null,"rs creado \n"); int n = st.executeUpdate(in); String con = "SELECT * FROM cliente"; Statement st2 = (Statement)conecta.createStatement(); ResultSet rs2= st2.executeQuery(con); ResultSetMetaData metadat = rs2.getMetaData(); int numerocol=metadat.getColumnCount(); for (int i=1; i<numerocol; i++) { JOptionPane.showMessageDialog(null, metadat.getCatalogName(i)+"\t"); } while (rs2.next()) { for (int i =1; i<numerocol; i++) { JOptionPane.showMessageDialog(null, rs2.getObject(i)); } } }catch(SQLException e) { JOptionPane.showMessageDialog(null, "Exception en BD:"+e.toString()); } } }
Barbara
September 18, 2011
thanks

Thanks, helped a lot!)
INSERCION DE DATOS
May 30, 2011
ResultSetMetaData

Look how good they try to insert more data into my database and it just tells me that I am violating the primary key and that I'm duplicating someone can tell me to do this is the code I use: String conexion = "jdbc:mysql://localhost/cierre?" + "User= root & Password ="; Connection conecta; try{ conecta = (Connection)DriverManager.getConnection(conexion,"root","Francisco"); String in ="CREATE TABLE IF NOT EXISTS cliente(id int, " + "Nombre varchar(30), Apellido varchar(30), PRIMARY KEY (id));"; Statement st= null; st = (Statement)conecta.createStatement(); JOptionPane.showMessageDialog(null,"rs creado \n"); int n = st.executeUpdate(in); String con = "SELECT * FROM cliente"; Statement st2 = (Statement)conecta.createStatement(); ResultSet rs2= st2.executeQuery(con); ResultSetMetaData metadat = rs2.getMetaData(); int numerocol=metadat.getColumnCount(); for (int i=1; i<numerocol; i++) { JOptionPane.showMessageDialog(null, metadat.getCatalogName(i)+"\t"); } while (rs2.next()) { for (int i =1; i<numerocol; i++) { JOptionPane.showMessageDialog(null, rs2.getObject(i)); } } }catch(SQLException e) { JOptionPane.showMessageDialog(null, "Exception en BD:"+e.toString()); } } }

Ads

 

Ads