package com.JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCExample {
public static void main(String arg[]) {
Connection connection = null;
//Register the driver class
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("MySQL JDBC Driver Registered!");
//Creating connection
// driver //service url / DBName , username, pwd
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
System.out.println("SQL Connection to database established!");
Statement stmt = connection.createStatement();
ResultSet rows = stmt.executeQuery("select * from users");
while (rows.next())
{
System.out.println(rows.getInt(1));
System.out.println(rows.getString("username"));
System.out.println(rows.getString("password"));
}
//Creating thePrepare Statement
PreparedStatement pstmt = connection.prepareStatement("insert into users values (?,?,?)");
pstmt.setInt(1, 3);
pstmt.setString(2, "Murali");
pstmt.setString(3, "pswd1");
//Executing the Query
int norows = pstmt.executeUpdate();
System.out.println("No. of rows inserted; " +norows);
}
catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found !!");
return;
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
return;
} finally {
try
{
if(connection != null)
connection.close();
System.out.println("Connection closed !!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCExample {
public static void main(String arg[]) {
Connection connection = null;
//Register the driver class
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("MySQL JDBC Driver Registered!");
//Creating connection
// driver //service url / DBName , username, pwd
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
System.out.println("SQL Connection to database established!");
Statement stmt = connection.createStatement();
ResultSet rows = stmt.executeQuery("select * from users");
while (rows.next())
{
System.out.println(rows.getInt(1));
System.out.println(rows.getString("username"));
System.out.println(rows.getString("password"));
}
//Creating thePrepare Statement
PreparedStatement pstmt = connection.prepareStatement("insert into users values (?,?,?)");
pstmt.setInt(1, 3);
pstmt.setString(2, "Murali");
pstmt.setString(3, "pswd1");
//Executing the Query
int norows = pstmt.executeUpdate();
System.out.println("No. of rows inserted; " +norows);
}
catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found !!");
return;
}
catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
return;
} finally {
try
{
if(connection != null)
connection.close();
System.out.println("Connection closed !!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
No comments:
Post a Comment