0

i would like to ask how to connect Java program in MySQL, help me please! and can you give me a demo just to show the records on Database, thanks in advance!

1 Answers1

1

the 127.0.0.1:3306 is depends what is the settings of your database make sure you add the MySQL JDBC Driver in your Libraries then try this code..

try{
    Class.forName("com.mysql.jdbc.Driver"); // to set the driver
    Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306", "<Database username>", "<Database password>"); // connect to database
    Statement st = connect.createStatement(); 
    Resultset rs = st.executeQuery( "SELECT * FROM <tablename>" );

    while( rs.next() ){
            System.out.print( rs.getString(1) );
    }
}
catch( Exception e ){
    System.out.print( e );
}
wrecklez
  • 343
  • 2
  • 4