I'm new in this field, and trying to do some tests using java in Ubuntu
I wrote a code in Java to connect my localhost mssql server and create a new database. the code is tested on windows and it's working expectedly, i copied pasted in Ubuntu server 22.04 servercompiled it using command: javac db_connect.javaran the following command: java db_connect
once i run the following command java db_connect I'm getting the following errors
Error: Could not find or load main class db_connect Caused by: java.lang.NoClassDefFoundError: db_connection/db_connect (wrong name: db_connect)
my main public class name is db_connect.
what should I change in the code or add modules to try to run the compiled java file.
in advance thank you!
find the code below:
package db_connection;import java.sql.*;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class db_connect { void func1() { String url =("jdbc:sqlserver://localhost:1433;integratedSecurity=true;"); String user=""; String password = ""; try(Connection conn = DriverManager.getConnection(url,user,password)) { Statement stmt = conn.createStatement(); String sql1 = "create database TM_users"; stmt.executeUpdate(sql1); System.out.println("Database created successfully..."); //check if DB Exists String checkDBName = "bd1"; ResultSet rs = conn.getMetaData().getCatalogs(); boolean dbExists = false; while (rs.next()) { String databaseName = rs.getString(1); if (databaseName.equalsIgnoreCase(checkDBName)) { dbExists = true; break; } } rs.close(); if (dbExists) { System.out.println("Database already exists."); }else { String sql = "CREATE DATABASE db_1"; stmt.executeUpdate(sql); System.out.println("Database 'db_1' created successfully."); } } catch(Exception e) { System.out.println("Err =>" + e.getMessage()); } } public static void main(String[] args) throws SQLException { db_connect ob1 = new db_connect(); ob1.func1(); }}