Creating and connecting database with JAVA

Asked By 0 points N/A Posted on -
qa-featured

Hello, I have a question about how can I create a database with Java and how can I also connect that database to my Application or Software.

What are the keys to create and connect the Database to my Application or Software?

Can you give me an Example ?

Thanks !

SHARE
Best Answer by dynamo
Best Answer
Best Answer
Answered By 0 points N/A #126109

Creating and connecting database with JAVA

qa-featured

Hi,

I think this java code will help you create a database from java

import java.io.*;
import java.sql.*;

public class CreateDatabase{
  public static void main(String[] args) {
  System.out.println("Database creation example!");
  Connection con = null; //first you have to create a connection
  try{
  Class.forName("com.mysql.jdbc.Driver");//then specify the connection name or the jdbc driver
  con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/databaseName","userName","password");/*after that, you have to connect to your RDBMS specifying the URL, username and password*/
  try{
  Statement st = con.createStatement();//buffer reader to
  BufferedReader bf = new BufferedReader//get the database name from keyboard
(new InputStreamReader(System.in));
  System.out.println("Enter Database name:");
  String database = bf.readLine();
  st.executeUpdate("CREATE DATABASE "+database);//actual creation of the database
  }
  catch (SQLException s){
  System.out.println("SQL statement is not executed!");
  }
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
}

This code will help you connect to a database :

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";//Server URL
  String dbName = "dataBaseName";//DataBase name
  String driver = "com.mysql.jdbc.Driver";//The driver class
  String userName = "root"; //User name
  String password = "root";//The password
  try {
  Class.forName(driver).newInstance();//Create new instance
  conn = DriverManager.getConnection(url+dbName,userName,password);//Connect to the database
  System.out.println("Connected to the database");
  conn.close();//Close the connection to the database
  System.out.println("Disconnected from database");
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
}

Hope this will help.

P.S. If you have any other questions don't hesitate 🙂

Answered By 0 points N/A #126110

Creating and connecting database with JAVA

qa-featured

 

Hi Jay

First I am Writing a program related to database(JDBC/ODBC) connectivity. First go through the program then below are the written steps how to make connectivity.

 

import java.sql.*;
 
class Table{
public static void main(String args[])
 
{
Connection conn;
 
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:Table");
Statement stmt=conn.createStatement();
ResultSet r=stmt.executeQuery("SELECT * FROM emp");
 
while(r.next())
{
System.out.println("Employee ID:"+r.getInt("1"));
System.out.println("Employee Name:"+r.getString("2"));
System.out.println("Employee phone:"+r.getString("3"));
System.out.println("Employee designation:"+r.getString("4"));
System.out.println("Employee salary:"+r.getInt("5"));
}
}
 
catch(Exception e)
{
System.out.print("Error:" +e.getMessage());
}
}
}
 
Follow the steps :
 
1) Go to Control panel and select the option Data Sources(ODBC)
 
 
 
2) A dialog box will appear as follows
 

3) Click on Add. A dialog box will appear, Select Microsoft Access Driver(*.mdb,*.accdb) from the list

4) Click on Finish button. A dialog box will appear. Enter the Data Source Name. Data Source name is the name you will use in the program. Like in the above program in the statement conn=DriverManager.getConnection("jdbc:odbc:Table"); Table is the data source name. It is necessary whatever name you will enter you have to write the same in the program.

5) After this click on the select button. Select the database you have created. For example in the above program i have created a database record in Microsoft access named emp. Now when you will write the queries you have to write the same name of the database. When you will click on select button a new dialog box will appear in which you will select the location of your database.

These are the steps of connecting database. I am sending you a snap of database. Just create the same database in Microsoft Access just for example and save it with the name emp and do the above steps then run the java program.

Feel free to ask any qoustion if you still have any dought.

Related Questions