Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //You have to add "mysql-connector-java.jar" library
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class SQL_Sample_1 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String verbo = "";
- System.out.println("SCRIVIMI UN VERBO ED IO TI DARO' IL SINONIMO");
- verbo = input.nextLine();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException ex) { ex.printStackTrace(); }
- Connection conn = null;
- try {
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseName", "root", ""); //Connecting to the database
- }
- catch (SQLException ex) { ex.printStackTrace(); }
- Statement stmt = null;
- try { stmt = conn.createStatement();}
- catch (SQLException ex) { ex.printStackTrace(); }
- String query = "select azione.Azione"
- + " from sinonimo inner join azione on sinonimo.CodiceAzione = azione.Codice"
- + " where sinonimo = '" + verbo + "'";
- //Executing the query
- try {
- ResultSet rs = stmt.executeQuery(query);
- //Fetching results
- while(rs.next())
- {
- String azione = rs.getString("azione");
- System.out.println(azione);
- }
- } catch (SQLException ex) { ex.printStackTrace(); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement