How to connect MySQL DataBase to Java Application

Joined
Nov 6, 2023
Messages
11
Reaction score
0
I have a mysql database winx and I want to insert values in table from user and to display them using a java application
 
Joined
Nov 5, 2023
Messages
5
Reaction score
1
public class MainApp { public static void adaugare_winx(Connection conn){
Scanner scanner=new Scanner(System.in); String name=scanner.next();
int age=scanner.nextInt(); String birthday=scanner.next();
String comanda="insert into fairies (name, age, birthday) values (?,?,?)"; try(PreparedStatement prep=conn.prepareStatement(comanda)){
prep.setString(1,name); prep.setInt(2,age);
prep.setString(3, birthday); prep.executeUpdate();
}catch(SQLException e){ e.printStackTrace();
} }
public static void afisare(Connection conn)
{ String comanda="select * from fairies";
try(Statement statement=conn.createStatement()){ ResultSet resultSet=statement.executeQuery(comanda);
while(resultSet.next()){ int id=resultSet.getInt("id");
String name=resultSet.getString("name"); int age=resultSet.getInt("age");
String birthday=resultSet.getString("birthday"); System.out.println(id+ " "+name+" "+age+" "+birthday);
} }catch(SQLException e){
e.printStackTrace(); }
} public static void main(String[] args) throws SQLException {
try{ String url = "jdbc:mysql://localhost:3306/winx";
Connection connection= DriverManager.getConnection(url, "root", ""); adaugare_winx(connection);
afisare(connection);
}catch (SQLException e){ e.printStackTrace();
} }
}
 
Joined
Nov 5, 2023
Messages
5
Reaction score
1
public class MainApp {
public static void adaugare_winx(Connection conn){
Scanner scanner=new Scanner(System.in); String name=scanner.next();
int age=scanner.nextInt(); String birthday=scanner.next();
String comanda="insert into fairies (name, age, birthday) values (?,?,?)";

try(PreparedStatement prep=conn.prepareStatement(comanda)){
prep.setString(1,name); prep.setInt(2,age);
prep.setString(3, birthday); prep.executeUpdate();
}
catch(SQLException e)
{ e.printStackTrace();}
}

public static void afisare(Connection conn)
{ String comanda="select * from fairies";
try(Statement statement=conn.createStatement()){
ResultSet resultSet=statement.executeQuery(comanda);
while(resultSet.next()){ int id=resultSet.getInt("id");
String name=resultSet.getString("name"); int age=resultSet.getInt("age");
String birthday=resultSet.getString("birthday"); System.out.println(id+ " "+name+" "+age+" "+birthday);
} }
catch(SQLException e){
e.printStackTrace(); }
}
public static void main(String[] args) throws SQLException {
try{ String url = "jdbc:mysql://localhost:3306/winx";
Connection connection= DriverManager.getConnection(url, "root", ""); adaugare_winx(connection);
afisare(connection);
}catch (SQLException e){ e.printStackTrace();
} }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top