3. Java Database Connectivity (JDBC)
3.1 Develop a database application that uses any JDBC driver
I used Netbeans
Student table
JDBCDemo2.class
package jdbcdemo2;import java.sql.*;/**** @author arpit*/public class JDBCDemo2 {/*** @param args the command line arguments*/public static void main(String[] args) {Connection con = null;try {Class.forName("com.mysql.cj.jdbc.Driver");con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");Statement stat = con.createStatement();String sql = "SELECT * FROM student";ResultSet rs = stat.executeQuery(sql);System.out.println("Rollno | Name | Roll");System.out.println("=============================================");while (rs.next()) {System.out.println(rs.getInt("id") + " | " + rs.getString("name") + " | " + rs.getString("roll"));}rs.close();stat.close();con.close();} catch (ClassNotFoundException | SQLException e) {System.out.println("Exception: " + e.getMessage());}}}
0 Comments
Post a Comment