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!
Asked
Active
Viewed 65 times
0
-
possible duplicate of [Java connectivity with MySQL](http://stackoverflow.com/questions/2839321/java-connectivity-with-mysql) – R D Jun 30 '14 at 09:47
-
@RajavelD i cant understand that post – user3789690 Jun 30 '14 at 09:57
1 Answers
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