Aim:-
(Use of JDBC) WAP to – 1.Create a database using java. 2. Create a table in
the database. 3. Insert records in the table.
Software Used: -
Netbean, MariaDB
Coding:-
1. Create
a database using java. –
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<%
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306",
"root", "ekta");
Statement statement = connection.createStatement();
String query = "CREATE DATABASE test";
statement.executeUpdate(query);
out.println("Database test created
sucessfully.");
}
catch (Exception e)
{
out.println("An error occurred.");
}
%>
2. Create
a table in the database. –
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Creating a Table</title>
</head>
<body>
<h1>Creating a Table</h1>
<%
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "ekta");
Statement statement = connection.createStatement();
String query = "CREATE TABLE info (Id INTEGER, Name CHAR(50));";
statement.executeUpdate(query);
out.println("Table info create sucessfully.");
}
catch (Exception e)
{
out.println("An error occurred.");
}
%>
</body>
</html>
3. Insert
records in the table. –
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*,java.util.*"%>
<%
String ID=request.getParameter("ID");
String name=request.getParameter("name");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "ekta");
Statement st=conn.createStatement();
int i=st.executeUpdate("insert into
info(ID,name)values(001,'ekta')");
out.println("Data is successfully
inserted!");
}
catch(Exception e)
{
System.out.print(e);
e.printStackTrace();
}
%>
Output:
Theory:
What is data?
Data is any set of characters that is gathered from some purpose. It can be any character, including text and numbers, pictures, video, sound, etc.
Data is a collection of a distinct small unit of information. it can be stored in pieces of paper or electronic memory, etc.
What is database?
A database is an organized collection of structured information, or data, typically stored electronically in a computer system. A database is usually controlled by a database management system (DBMS).
The database is a collection of inter-related data which is used to retrieve insert delete the data efficiently
In other word Database is a shared collection of logically related data in a systematic manner that can be easily accessed, managed and updated.
Using the database you can retrieve, insert and delete the information
Database is actually a place where related piece of information is stored and various operations can be performed on it.
What is database management system?
Database management system is software which is used to manage the database. Ex My SQL, oracle, My SQL server etc.
DBMS provides an interface to perform various operations like database creation, creating table, storing data, updating data, etc.
A DBMS is a collection of programs that enables users to store, modify, and extract information from a database as per the requirement.
DBMS is used to perform any kind of operation on data in database.
Create Database statement
In order to create a new database on our server, we need to use the following command:
CREATE DATABASE database_name;
Ex.
CREATE DATABASE mydb;
After running this command, our mydb database is created, and you can see it in the databases list.
Create Table statement
A table is a structure used to store data in the database.
The CREATE TABLE statement is used to create a new table in a database.
To define a table, we need to use the following command:
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
INSERT INTO statement
The INSERT INTO statement is used to insert new records in a table.
For inserting values in the table, we need to use the following command:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
0 Comments
if you have any problem, please let me know