Académique Documents
Professionnel Documents
Culture Documents
Database Connection
Budditha Hettige Department of Statistics and Computer Science
Budditha Hettige
MySQL Database
world's most popular open source database http://www.mysql.com/
Budditha Hettige 2
Tools
PhpMyAdmin
Budditha Hettige
Budditha Hettige
Budditha Hettige
JDBC Driver1
JDBC Driver2
JDBC-ODBC Bridge
ODBC Driver
Data Source1
Data Source2
Budditha Hettige
Data Source3
7
JDBC API
Import the java.sql package
import java.sql.*;
Sample Codes
Create new MySQL Connection Insert new record Delete a record Update new record Show existing records
Budditha Hettige
Database Connection
// Manual connection for the localHost public Connection getConnection() throws Exception { Connection conn = null; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/testdb"; String username = root"; String password = ""; Class.forName(driver); // load MySQL driver conn = DriverManager.getConnection(url, username, password); return conn; }
Budditha Hettige
10
if (rowCount == 1) { System.out.println("new row insert"); } else { System.out.println("Not insert"); } } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } stmt.close(); return rowCount; }
Budditha Hettige 11
Show Records
public void ReadAllRecords() { try { Statement stmt = null; ResultSet rs = null; Connection MySqlConn = null; int age; String name; MySqlConn = getConnection(); stmt = MySqlConn.createStatement(); rs = stmt.executeQuery("SELECT * FROM student"); while (rs.next()) { age = rs.getInt("Age"); name = rs.getString("name"); System.out.println(name + ", " + age); } rs.close(); stmt.close(); MySqlConn.close(); } catch (Exception ex) { Logger.getLogger(MySQLConnection.class.getName()).log(Level.SEVERE, null, ex); } } Budditha Hettige 12
Delete Records
public void DeleteRows(String stuName) { Statement stmt = null; ResultSet rs = null; Connection MySqlConn; int rowCount = -1; try { MySqlConn = getConnection(); stmt = MySqlConn.createStatement(); int delete = stmt.executeUpdate("DELETE FROM student WHERE name ='"+ stuName + "'"); if(delete == 1) { System.out.println("Row is deleted."); } else { System.out.println("Row is not deleted."); } stmt.close(); } catch (Exception ex) { Logger.getLogger(MySQLConnection.class.getName()).log(Level.SEVERE, null, ex); } finally { }
Budditha Hettige 13
Update Records
public void updateAge(String name, int newAge ) { try { boolean result = false; Connection MySqlConn; Statement stmt = null; MySqlConn = getConnection(); String sql = "UPDATE student SET age = ? WHERE name = ?"; PreparedStatement prest = MySqlConn.prepareStatement(sql); prest.setInt(1, newAge); prest.setString(2, name); int executeUpdate = prest.executeUpdate(); if (executeUpdate > 0) { System.out.println("Record Updaqted"); } MySqlConn.close(); } catch (Exception ex) { Logger.getLogger(MySQLConnection.class.getName()).log(Level.SEVERE, null, ex); }
}
Budditha Hettige 14
Sample Program
public static void main(String[] args) { try { Connection MySqlConn; MySQLConnection con = new MySQLConnection(); // con.getConnection(); MySqlConn = con.getConnection(); con.InsertNewRecord("budditha", 34); con.InsertNewRecord("saman", 25); con.InsertNewRecord("kamal", 23); con.ReadAllRecords(); con.DeleteRows("saman"); con.updateAge("budditha", 35 ); con.ReadAllRecords(); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
Budditha Hettige
15