0

I wrote code that will take the input from JtextFields when a button is pressed, which will then put the values into query language and supposedly will add them

Ive tried googling and changing code many times, all to no prevail

addadmin.addActionListener(ev -> {
             String adminName = theAdminName.getText();
             String adminlastname = theAdminlastname.getText();

             String msAccDB = "C:/Users/joshu/Desktop/Linda Liu Database.accdb";
             String dbURL = "jdbc:ucanaccess://" + msAccDB; 

             Connection connection;
             Statement statement;
             ResultSet resultSet;
            try {
                connection = DriverManager.getConnection(dbURL);
                statement = connection.createStatement();
                resultSet = statement.executeQuery("INSERT INTO (AdminName, AdminLastName) VALUES (" + adminName + ", " + adminlastname + ");");
            } catch (SQLException e) {
                e.printStackTrace();
            } 
         }); 

I expect it to add the input from the buttons

exceptions java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:/Users/joshu/Desktop/Linda Liu Database.accdb at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source)

Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
Joshua Wu
  • 3
  • 2

2 Answers2

0

You miss the quotes:

"INSERT INTO (AdminName, AdminLastName) VALUES (‘" + adminName + "’, "’ + adminlastname + "’);"
Gustav
  • 43,657
  • 6
  • 27
  • 48
-1

From what I see, I assume you have not initialized the driver for your program. You may take the following code as the reference, it allows the program to connect to mssql server. You also can find detail on how to connect to Access in this tutorial

private Connection con;
private void insert()
{
    con = new Connection();
    String url = "jdbc:sqlserver://192.168.1.1:123456;instance=COMPUTER\\DD;databaseName=SERVERNAME";
    String UserName = "user";
    String pass = "123456";
    Connection con = null;
    try
    {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //This is the driver
        //Can be downloaded from Microsoft site
        con = DriverManager.getConnection(url, UserName, pass);
        //Driver initilize connection.
        String query = "INSERT INTO table"
        +"(column1, column2, column3)"
        + "VALUES"
        + "(?,?,?,?)";
        String column1 = jtfData1.getText();
        String column2 = jftData2.getText();
        String column3 = jtfData3.getText();
        String column4 = jtfData4.getText();
        //getting data from jtextfield
        try
        {
            PreparedStatement prep = con.prepareStatement(query);
            prep.setString(1, column1);
            prep.setString(2, column2);
            prep.setString(3, column3);
            prep.setString(4, column4);
            prep.executeQuery();
            con.close();
            //Query being execute accordingly
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
Duke
  • 23
  • 5
  • Your answer is about how to connect to Microsoft SQL Server, not Microsoft Access. They are totally different things. – Gord Thompson Aug 25 '19 at 01:00
  • 1
    Using `Class.forName` is unnecessary in normal Java programs using a JDBC 4 or higher driver, as then the driver will be loaded automatically. Explicitly loading the driver is only necessary when the driver isn't on the initial classpath (eg if the driver jar is inside a WAR). – Mark Rotteveel Aug 25 '19 at 06:16