Java with MySQL JDBC Connection Example Program
package com.practice.connection;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCPreparedStatementSelectExample {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost/student_db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "admin";
public static void main(String[] argv) {
try {
selectRecordsFromTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void selectRecordsFromTable() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String selectSQL = "SELECT * FROM student_profile";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(selectSQL);
//preparedStatement.setInt(1, 2);
// execute select SQL stetement
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
String userid = rs.getString("rollNo");
String username = rs.getString("name");
System.out.println("userid : " + userid);
System.out.println("username : " + username);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB_CONNECTION, DB_USER,DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCPreparedStatementSelectExample {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost/student_db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "admin";
public static void main(String[] argv) {
try {
selectRecordsFromTable();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
private static void selectRecordsFromTable() throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String selectSQL = "SELECT * FROM student_profile";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(selectSQL);
//preparedStatement.setInt(1, 2);
// execute select SQL stetement
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
String userid = rs.getString("rollNo");
String username = rs.getString("name");
System.out.println("userid : " + userid);
System.out.println("username : " + username);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB_CONNECTION, DB_USER,DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}
Comments
Post a Comment