Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Install MySql server in your machine
- Create the database as "test"
- create new table as "newtable"
- Java Project
- -------------
- - create a java project
- - Download the mysql java library "mysql-connector-java-5.1.23-bin.jar"
- - Add this jar into project library
- - Then copy the below code into the java class
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class Main {
- public static void main(String args[]){
- Connection connection = null;
- ResultSet resultset = null;
- Statement statement = null;
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String connectionUrl = "jdbc:mysql://localhost:3306/test";
- String connectionUser = "root";
- String connectionPassword = "welcome123";
- connection = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
- statement = connection.createStatement();
- resultset = statement.executeQuery("SELECT * FROM newtable");
- while (resultset.next()) {
- int id = resultset.getInt("id");
- String name = resultset.getString("name");
- System.out.println("ID: " + id + ", Name: " + name);
- }
- }catch(Exception e){
- }finally {
- try{
- if(connection != null)
- connection.close();
- if(statement != null)
- statement.close();
- if(resultset != null)
- resultset.close();
- }catch(Exception ie){
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement