السلام عليكم ورحمة الله و بركاته
ابي مساعده لمشكلتي , عندي مشروع تخرج وعندي مشكله في الربط my sql with java beans
هذا مثال على ربط و طلع لي فيه error , حتى انشاء اتصال يقاعدة البيانات يطلع لي error ما ينشئ اتصال بكلمة مرور و كلمة مستخدم
هذا الكود :
السلام عليكم ورحمة الله و بركاته
ابي مساعده لمشكلتي , عندي مشروع تخرج وعندي مشكله في الربط my sql with java beans
هذا مثال على ربط و طلع لي فيه error , حتى انشاء اتصال يقاعدة البيانات يطلع لي error ما ينشئ اتصال بكلمة مرور و كلمة مستخدم
هذا الكود :
//STEP 1. Import required packages
import java.sql.*;
public class example {
/**
*
* @param args
*/
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
/**
*
* @param args
*/
public static void main(String[] args) {
// JDBC driver name and database URL
public class JDBCExample {
public void main(String[] args) throws ClassNotFoundException {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT id, first, last, age FROM Registration";
try (ResultSet rs = stmt.executeQuery(sql)) {
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}}
}catch(SQLException se){
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample }
}