TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Insert Data from Database in Java
Pintoo Yadav
Nov 30
2015
Code
1.5
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package logic;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @author Pintoo
*/
public
class
logic {
static
final String JDBC_DRIVER =
"com.microsoft.sqlserver.jdbc.SQLServerDriver"
;
static
final String DB_URL =
"jdbc:sqlserver://192.168.100.253;databaseName=golivecode"
;
static
final String USER =
"sa"
;
static
final String PASS =
"admin@123"
;
Connection conn =
null
;
Statement stmt =
null
;
ResultSet rs =
null
;
public
static
void
main(String[] args) {
logic logic1 =
new
logic();
// logic1.Delete("pinto");
}
public
void
insert(String name, String pass)
{
try
{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String query =
"insert into Texttbl values ('"
+ name +
"','"
+ pass +
"')"
;
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(
null
,
"A message from Java Servlet."
);
}
catch
(Exception e)
{
e.printStackTrace();
System.
out
.println(
"Error connecting databsse"
+ e.getStackTrace());
}
finally
{
try
{
if
(conn !=
null
)
{
conn.close();
}
if
(stmt !=
null
)
{
stmt.close();
}
}
catch
(Exception e)
{
System.
out
.println(e.getStackTrace());
}
}
}
public
void
Delete(String name)
{
try
{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String query =
"Delete From Texttbl where name='"
+ name +
"'"
;
stmt.executeUpdate(query);
//JOptionPane.showMessageDialog(this, "Saved");
// rs=stmt.executeQuery("select * from AccountMaster");
// while(rs.next()){
// System.out.println(rs.getString("CustomerName"));
//}
}
catch
(Exception e)
{
e.printStackTrace();
System.
out
.println(
"Error connecting databsse"
+ e.getStackTrace());
}
finally
{
try
{
if
(conn !=
null
)
{
conn.close();
}
if
(stmt !=
null
)
{
stmt.close();
}
}
catch
(Exception e)
{
System.
out
.println(e.getStackTrace());
}
}
}
public
void
select()
{
try
{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
//JOptionPane.showMessageDialog(this, "Saved");
rs = stmt.executeQuery(
"select * from AccountMaster"
);
while
(rs.next()) {
System.
out
.println(rs.getString(
"CustomerName"
));
}
}
catch
(Exception e)
{
e.printStackTrace();
System.
out
.println(
"Error connecting databsse"
+ e.getStackTrace());
}
finally
{
try
{
if
(conn !=
null
)
{
conn.close();
}
if
(stmt !=
null
)
{
stmt.close();
}
}
catch
(Exception e)
{
System.
out
.println(e.getStackTrace());
}
}
}
}
java
database
Insert Data from Database