Java and MySQL

Home » Blog » Java and MySQL
I tried to write very simple program with JAva and MySQL. Code is below. To use mysql in java, you need to download MySQL Java Driver. Next, include .jar file to your project
import java.sql.*;

public class Lesson28 {

public static void main(String[] args ) {

Connection conn = null;

try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/world", "root", "xxxxxxxx");

Statement sqlState = conn.createStatement();

String selectStuff = "Select * from city limit 10";

ResultSet rows = sqlState.executeQuery(selectStuff);

while(rows.next()) {
System.out.println(rows.getString("Name"));
}
} catch(SQLException ex) {
System.out.println("SQL Exception:" + ex.getMessage());
System.out.println("VendorError:" + ex.getErrorCode());
} catch(ClassNotFoundException e) {
e.printStackTrace();
}

}

}

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This