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!

2
  • possible duplicate of Java connectivity with MySQL Commented Jun 30, 2014 at 9:47
  • @RajavelD i cant understand that post Commented Jun 30, 2014 at 9:57

1 Answer 1

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 );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.