Question

I just started taking my first steps in sql.
I downloaded and installed XAMPP 1.8.3-3, and also downloaded the JDBC driver and wrote this code:

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class TestSQL {

    public static void main(String[] args) {
        Connection connection=null;     

        try {
            MysqlDataSource ds=new MysqlConnectionPoolDataSource();
            ds.setServerName("localhost");
            connection=ds.getConnection("root", "");
            Statement statement=connection.createStatement();
            statement.execute("CREATE DATABASE my_database;");
            statement.execute("USE my_database;");
            statement.execute("CREATE TABLE my_table (" +
                                "private_name VARCHAR(10)," +
                                "last_name VARCHAR(10)," +
                                "age INT" +
                                ");");
            statement.execute("INSERT INTO my_table" +
                    "(private_name,last_name,age)" +
                    "VALUES " +
                    "('Ariel','Menashe',29)");
            statement.execute("INSERT INTO my_table" +
                    "(private_name,last_name,age)" +
                    "VALUES " +
                    "('Aviad','Ganon',28)");
            statement.execute("INSERT INTO my_table" +
                    "(private_name,last_name,age)" +
                    "VALUES " +
                    "('Oren','Vanunu',30)");
            ResultSet rs=statement.executeQuery("SELECT * FROM my_table");

            while (rs.next()) {
                System.out.println("Mr. "+rs.getString("last_name")+", "+rs.getInt("age"));
            }

            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

This code will compile and run, but my question is, how do I write code in SQL without java? can I run SQL commands through the terminal independently somehow?

Was it helpful?

Solution

Sure you can. If you have the MySQL command line client installed, you can start it up in a terminal and run any SQL command you like. Type this in your terminal:

mysql databasename -h host -u username -p

It will ask for the password interactively. If your database is on the same server as the client, you can leave off the -h host part.

OTHER TIPS

You can issue SQL directly to a database by connecting to it. At a minimum, you'll need to know the username and password for the database, and (in some cases) the port if the database is not running on its standard port (MySQL usually runs on 3306). Different users have different permissions (for example read only vs add/drop tables), but you'll be able to do anything directly that you're able to do with the credentials that you connected with in your JDBC code. See here for the MySQL docs on how to connect. You might also check out MySQL Workbench, which is a GUI for connecting to MySQL and viewing the contents of a database.

SQL developer will help you run and explore sql with out needing to run code.

Its free and from oracle, for mysql database there is a provider: http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index.html http://www.oracle.com/technetwork/developer-tools/sql-developer/thirdparty-095608.html

There are several other IDE's for sql that may be better suited for mysql.

Have a look at https://stackoverflow.com/questions/259157/what-is-your-recommendation-for-a-good-sql-ide .

SQL is a Language that is used in order to retrieve, modify and/or delete data from a Database. It is not directly associated with any other particular language, as it is a language itself.

Usually, there are APIs in several languages in order to do actions in a database in a more transparent way or even to allow executing SQL commands in a database. Databases are organized in Systems called Database Management Systems (DBMS), that allow good management on a large set of databases.

If you are using a Database Management System, usually, you can install a command prompt to execute SQL commands. This prompt can usually be provided via command line, an installed native application or a web application administration panel. A simple yet good DBMS is MySQL, and a good tool to manage MySQL DBMS is PHPMyAdmin.

The most useful to use some plugins to the IDE to run a database console. The one that supplied with IntelliJ IDEA is jdbc-console.jar. It only requires util.jar to be on classpath. You can find this from the task manager when you press on SQL button of the database flyer. Other IDEs like Eclipse have similar console applications implied as plugins. You can write your own jdbc-like console application that accept a string and keyboard shortcuts. Some databases like Oracle use a specific client application installed with the database client written in Java: SQL Developer. MySQL Workbench is heavy on start their instances.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top