How to update DataBase using parameters via ODBC? - mysql

Actually i have a method where i insert some data to MySQL database via ODBC and when i get an exception i would try to update the value in the DB as most of the cases it would mean that the value yet exist.
The issue is that how can i update the DB using parameters? actually in Insert i use "?" as values and it works fine while in update it doesn't work and even by using param names it has no effects.
Here is my code
Dim sqlInsert As String = "INSERT INTO prodotti(`Cod Articolo`, `Descrizione`, `Prezzo vend`, `Prezzo Acq`, `Iva`, `Data UV`, `Reparto`, `Descrizione Reparto`, `Famiglia/Gruppo`,
`Descrizione Famiglia/Gruppo`, `Settore`, `Descrizione Settore`, `Marca`, `Cod Forn`, `Descrizione Fornitore`, `Sconto`, `RC`, `Prezzo EURO`, `Tipo_Frontal`,
N_Frontal, `Tipo Riord`, `UM`, `Unita`, `Prezzo Unita`, `Peso_Unit`, `Unita_Misura`, `Data_Crea`, `Punti Promozione`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
Dim sqlUpdate As String = "UPDATE prodotti SET Descrizione = #CodArticolo, `Prezzo vend` = #Prezzovend, `Prezzo Acq` = #PrezzoAcq, Iva = #Iva, `Data UV` = #DataUV,
Reparto = #Reparto, `Descrizione Reparto` = #DescrizioneReparto, `Famiglia/Gruppo` = #FamigliaGruppo, `Descrizione Famiglia/Gruppo` = #DescrizioneFamigliaGruppo, Settore = #Settore,
`Descrizione Settore` = #DescrizioneSettore, Marca = #Marca, `Punti Promozione` = #PuntiPromozione, `Cod Forn` = #CodForn, `Descrizione Fornitore` = #DescrizioneFornitore,
Sconto = #Sconto, Tipo_Frontal = #Tipo_Frontal, N_Frontal = #N_Frontal, `Tipo Riord` = #TipoRiord, UM = #UM, `Unita` = #Unita, `Prezzo Unita` = #PrezzoUnita, `Peso_Unit` = #PesoUnita, `Unita_Misura` = #Unita_Misura
WHERE `Cod Articolo` = #CodArticolo"
Try
dbCon.Open()
For Each Row As DataRow In data.Rows
Dim cmd As New Odbc.OdbcCommand
With cmd
.CommandType = CommandType.Text
.CommandText = sqlInsert
.Connection = dbCon
.Parameters.AddWithValue("#CodArticolo", Row.Item("Cod Articolo"))
.Parameters.AddWithValue("#Descrizione", Row.Item("Descrizione"))
.Parameters.AddWithValue("#Prezzovend", Row.Item("Prezzo vend"))
.Parameters.AddWithValue("#PrezzoAcq", Row.Item("Prezzo Acq"))
.Parameters.AddWithValue("#Iva", Row.Item("Iva"))
.Parameters.AddWithValue("#DataUV", Row.Item("Data UV"))
.Parameters.AddWithValue("#Reparto", Row.Item("Reparto"))
.Parameters.AddWithValue("#DescrizioneReparto", Row.Item("Descrizione Reparto"))
.Parameters.AddWithValue("#FamigliaGruppo", Row.Item("Famiglia/Gruppo"))
.Parameters.AddWithValue("#DescrizioneFamigliaGruppo", Row.Item("Descrizione Famiglia/Gruppo"))
.Parameters.AddWithValue("#Settore", Row.Item("Settore"))
.Parameters.AddWithValue("#DescrizioneSettore", Row.Item("Descrizione Settore"))
.Parameters.AddWithValue("#Marca", Row.Item("Marca"))
.Parameters.AddWithValue("#CodForn", Row.Item("Cod Forn"))
.Parameters.AddWithValue("#Reparto", Row.Item("Reparto"))
.Parameters.AddWithValue("#DescrizioneFornitore", Row.Item("Descrizione Fornitore"))
.Parameters.AddWithValue("#Sconto", Row.Item("Sconto"))
.Parameters.AddWithValue("#RC", Row.Item("RC"))
.Parameters.AddWithValue("#PrezzoEURO", Row.Item("Prezzo EURO"))
.Parameters.AddWithValue("#Tipo_Frontal", Row.Item("Tipo_Frontal"))
.Parameters.AddWithValue("#N_Frontal", Row.Item("N_Frontal"))
.Parameters.AddWithValue("#TipoRiord", Row.Item("Tipo Riord"))
.Parameters.AddWithValue("#UM", Row.Item("UM"))
.Parameters.AddWithValue("#Unita", Row.Item("Unita"))
.Parameters.AddWithValue("#PrezzoUnita", Row.Item("Prezzo Unita"))
.Parameters.AddWithValue("#Peso_Unit", Row.Item("Peso_Unit"))
.Parameters.AddWithValue("#Unita_Misura", Row.Item("Unita_Misura"))
.Parameters.AddWithValue("#Data_Crea", Row.Item("Data_Crea"))
.Parameters.AddWithValue("#PuntiPromozione", Row.Item("Punti Promozione"))
End With
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
Try
cmd.CommandText = sqlUpdate
cmd.ExecuteNonQuery()
Catch e As Exception
MsgBox("game over")
End Try
End Try
Next Row
Catch ex As Exception
Finally
dbCon.Close()
End Try
UPDATE
As suggested by #Mary i've declared Parameters outside the loop and initialized them inside it.
SQLBindParameter
Dim sqlInsert As String = "INSERT INTO prodotti(`Descrizione`, `Prezzo vend`, `Prezzo Acq`, `Iva`, `Data UV`, `Reparto`, `Descrizione Reparto`, `Famiglia/Gruppo`,
`Descrizione Famiglia/Gruppo`, `Settore`, `Descrizione Settore`, `Marca`, `Cod Forn`, `Descrizione Fornitore`, `Sconto`, `RC`, `Prezzo EURO`, `Tipo_Frontal`,
`N_Frontal`, `Tipo Riord`, `UM`, `Unita`, `Prezzo Unita`, `Peso_Unit`, `Unita_Misura`, `Data_Crea`, `Punti Promozione`, `Cod Articolo`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
Dim sqlUpdate As String = "UPDATE prodotti SET `Descrizione` = ?, `Prezzo vend` = ?, `Prezzo Acq` = ?, `Iva` = ?, `Data UV` = ?,
`Reparto` = ?, `Descrizione Reparto` = ?, `Famiglia/Gruppo` = ?, `Descrizione Famiglia/Gruppo` = ?, `Settore` = ?,
`Descrizione Settore` = ?, `Marca` = ?, `Cod Forn` = ?, `Descrizione Fornitore` = ?, `Sconto` = ?, `RC` = ?, `Prezzo EURO` = ?,
`Tipo_Frontal` = ?, `N_Frontal` = ?, `Tipo Riord` = ?, `UM` = ?, `Unita` = ?, `Prezzo Unita` = ?, `Peso_Unit` = ?, `Unita_Misura` = ?, `Data_Crea` = ?, `Punti Promozione` = ?
WHERE `Cod Articolo` = ?"
Dim cmd As New Odbc.OdbcCommand
With cmd
.Connection = dbCon
.CommandType = CommandType.Text
.Parameters.Add("#Descrizione", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#Prezzovend", odbcType:=Odbc.OdbcType.Double)
.Parameters.Add("#PrezzoAcq", odbcType:=Odbc.OdbcType.Double)
.Parameters.Add("#Iva", odbcType:=Odbc.OdbcType.Double)
.Parameters.Add("#DataUV", odbcType:=Odbc.OdbcType.DateTime)
.Parameters.Add("#Reparto", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#DescrizioneReparto", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#FamigliaGruppo", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#DescrizioneFamigliaGruppo", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#Settore", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#DescrizioneSettore", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#Marca", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#CodForn", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#DescrizioneFornitore", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#Sconto", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#RC", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#PrezzoEURO", odbcType:=Odbc.OdbcType.Double)
.Parameters.Add("#Tipo_Frontal", odbcType:=Odbc.OdbcType.Int)
.Parameters.Add("#N_Frontal", odbcType:=Odbc.OdbcType.Int)
.Parameters.Add("#TipoRiord", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#UM", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#Unita", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#PrezzoUnita", odbcType:=Odbc.OdbcType.Double)
.Parameters.Add("#Peso_Unit", odbcType:=Odbc.OdbcType.Double)
.Parameters.Add("#Unita_Misura", odbcType:=Odbc.OdbcType.VarChar)
.Parameters.Add("#Data_Crea", odbcType:=Odbc.OdbcType.DateTime)
.Parameters.Add("#PuntiPromozione", odbcType:=Odbc.OdbcType.Int)
.Parameters.Add("#CodArticolo", odbcType:=Odbc.OdbcType.VarChar)
End With
Try
dbCon.Open()
For Each Row As DataRow In data.Rows
cmd.CommandText = sqlInsert
cmd.Parameters("#CodArticolo").Value = Row.Item("Cod Articolo")
cmd.Parameters("#Descrizione").Value = Row.Item("Descrizione")
If IsDBNull(Row.Item("Prezzo vend")) Then
cmd.Parameters("#Prezzovend").Value = DBNull.Value
Else
cmd.Parameters("#Prezzovend").Value = Double.Parse(Row.Item("Prezzo vend"))
End If
If IsDBNull(Row.Item("Prezzo Acq")) Then
cmd.Parameters("#PrezzoAcq").Value = DBNull.Value
Else
cmd.Parameters("#PrezzoAcq").Value = Double.Parse(Row.Item("Prezzo Acq"))
End If
If IsDBNull(Row.Item("Iva")) Then
cmd.Parameters("#Iva").Value = DBNull.Value
Else
cmd.Parameters("#Iva").Value = Double.Parse(Row.Item("Iva"))
End If
If IsDBNull(Row.Item("Data UV")) Then
cmd.Parameters("#DataUV").Value = DBNull.Value
Else
cmd.Parameters("#DataUV").Value = New Date(Row.Item("Data UV"))
End If
cmd.Parameters("#Reparto").Value = Row.Item("Reparto")
cmd.Parameters("#DescrizioneReparto").Value = Row.Item("Descrizione Reparto")
cmd.Parameters("#FamigliaGruppo").Value = Row.Item("Famiglia/Gruppo")
cmd.Parameters("#DescrizioneFamigliaGruppo").Value = Row.Item("Descrizione Famiglia/Gruppo")
cmd.Parameters("#Settore").Value = Row.Item("Settore")
cmd.Parameters("#DescrizioneSettore").Value = Row.Item("Descrizione Settore")
cmd.Parameters("#Marca").Value = Row.Item("Marca")
cmd.Parameters("#CodForn").Value = Row.Item("Cod Forn")
cmd.Parameters("#DescrizioneFornitore").Value = Row.Item("Descrizione Fornitore")
cmd.Parameters("#Sconto").Value = Row.Item("Sconto")
cmd.Parameters("#RC").Value = Row.Item("RC")
If IsDBNull(Row.Item("Prezzo EURO")) Then
cmd.Parameters("#PrezzoEURO").Value = DBNull.Value
Else
cmd.Parameters("#PrezzoEURO").Value = Double.Parse(Row.Item("Prezzo EURO"))
End If
If IsDBNull(Row.Item("Tipo_Frontal")) Then
cmd.Parameters("#Tipo_Frontal").Value = DBNull.Value
Else
cmd.Parameters("#Tipo_Frontal").Value = CInt(Row.Item("Tipo_Frontal"))
End If
If IsDBNull(Row.Item("N_Frontal")) Then
cmd.Parameters("#N_Frontal").Value = DBNull.Value
Else
cmd.Parameters("#N_Frontal").Value = CInt(Row.Item("N_Frontal"))
End If
cmd.Parameters("#TipoRiord").Value = Row.Item("Tipo Riord")
cmd.Parameters("#UM").Value = Row.Item("UM")
cmd.Parameters("#Unita").Value = Row.Item("Unita")
If IsDBNull(Row.Item("Prezzo Unita")) Then
cmd.Parameters("#PrezzoUnita").Value = DBNull.Value
Else
cmd.Parameters("#PrezzoUnita").Value = Double.Parse(Row.Item("Prezzo Unita"))
End If
If IsDBNull(Row.Item("Peso_Unit")) Then
cmd.Parameters("#Peso_Unit").Value = DBNull.Value
Else
cmd.Parameters("#Peso_Unit").Value = Double.Parse(Row.Item("Peso_Unit"))
End If
cmd.Parameters("#Unita_Misura").Value = Row.Item("Unita_Misura")
If IsDBNull(Row.Item("Data_Crea")) Then
cmd.Parameters("#Data_Crea").Value = DBNull.Value
Else
cmd.Parameters("#Data_Crea").Value = New Date(Row.Item("Data_Crea"))
End If
If IsDBNull(Row.Item("Punti Promozione")) Then
cmd.Parameters("#PuntiPromozione").Value = DBNull.Value
Else
cmd.Parameters("#PuntiPromozione").Value = CInt(Row.Item("Punti Promozione"))
End If
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
Try
cmd.CommandText = sqlUpdate
cmd.ExecuteNonQuery()
Catch e As Exception
MsgBox("hai la mamma troia")
End Try
End Try
Next Row
Catch ex As Exception
Finally
dbCon.Close()
End Try
PS: i'm new in VB.NET

Use Using...End Using blocks for your database objects if the expose a .Dispose method. They need to release unmanaged objects. The Using blocks accomplish this even if there is an error.
.AddWithValue can cause some problems. See http://www.dbdelta.com/addwithvalue-is-evil/
and
https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
and another one:
https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications
Try the .Add(String, OdbcType, Int32) method. String is the parameter name. OdbcType is the the type of the column in the database. Int32 is the size of the field, relavent to string types. Convert the types from your datatable to match. ADO.net does not always guess correctly when filling a datatable.
'Example if the field is type Int
.Parameters.Add("#CodArticolo", OdbcType.Int).Value = CInt(Row.Item("Cod Articolo"))
'Example if the field is type VarChar
.Parameters.Add("#Descrizione", OdbcType.VarChar, 50).Value = Row.Item("Descrizione")
I don't understand the reason you cannot use the MySql provider.

Related

How can I create a stored procedure in mySQL without declaring all the parameters?

Probably a simple answer to this one but I can't find the answer online :(
I am currently migrating to MySQL database but I am having problems transfering my stored procedures from my dataset in Visual Studio. The SQL query in my dataset did not force me to name all the parameters however, MySQL gives a syntax error when I use ? instead of naming and declaring every single parameter.
SQL query in dataset of Visual Studio:
UPDATE tblConfigurations
SET DEUnit_ID_UnitName = ?, DEUnit_ID_UnitType = ?, DEUnit_ID_UnitLevel = ?, DEUnit_Aut_NumberOfMastercards = ?, DEUnit_Aut_MasterCardId1 = ?, DEUnit_Aut_MasterCardId2 = ?,
DEUnit_Aut_MasterCardId3 = ?, DEUnit_Aut_MasterCardId4 = ?, DEUnit_Aut_MasterCardId5 = ?, DEUnit_Aut_MasterCardId6 = ?, DEUnit_Aut_MasterCardId7 = ?, DEUnit_Aut_MasterCardId8 = ?,
DEUnit_Lck_SeperatedAntennas = ?, DEUnit_AF_SilentAlarmActivation = ?, DEUnit_AF_AlarmInputConnection = ?, DEUnit_NW_Address = ?, DEUnit_KD_KeyboardOrientation = ?,
DEUnit_KD_SpecialAcousticConfirmation = ?, DEUnit_KD_KeyClickSound = ?, DEUnit_KD_LedColors = ?, DEUnit_ReservedValue1 = ?, DEUnit_ReservedValue2 = ?, DEUnit_ReservedValue4 = ?,
DEUnit_ReservedValue7 = ?, DEUnit_ReservedValue8 = ?, DEUnit_ReservedValue9 = ?, DEUnit_ReservedValue10 = ?, DEUnit_ReservedValue11 = ?, DEUnit_ReservedValue12 = ?, DEUnit_ReservedValue13 = ?,
DEUnit_ReservedValue14 = ?, DEUnit_ReservedValue15 = ?, DEUnit_ReservedValue22 = ?, DEUnit_ReservedValue27 = ?
WHERE (config_ID = ?)
You can use the ? placeholders only if you use the PREPARE and EXECUTE syntax. Read https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html for details.
But even if you do use PREPARE & EXECUTE, you would still have to name the variables as you pass them to EXECUTE.
In a stored procedure, you can use variables instead of using placeholders.
CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)
BEGIN
SELECT COUNT(*) INTO cities FROM world.city
WHERE CountryCode = country;
END
In that example, country is a procedure input parameter. You can use it directly in an SQL statement (in this case SELECT, but it works for UPDATE too).
Make sure your parameter names are not the same as any column in the table you query, because that creates an ambiguity.

How to skip autoIncrement column to write data on mySQL db using JDBC Google Apps Script

I set up an Apps Script program and it needs to save data to database. However, it is not working.
I have tried to use the code from Google Apps Script Documentation but I can't see how to skip the autoIncrementing field named ID and its the primary key as well.
I have used code below.
var connectionName = 'frms-db:us-central1:****';
var user = 'rj-**';
var userPwd = 'fEwnj*LAAj3****';
var db = 'CCW_CLR';
var dbUrl = 'jdbc:google:mysql://' + connectionName + '/' + db;
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
var stmt2 = conn.prepareStatement('INSERT INTO tblPreInterview '
+ '(ID, Timestamp, Email, Section4, Section5, Section6, Section7, Pages12_13, MinorsAtHome, FirearmsStoreage, ReferenceType1, ReferenceName1, ReferencePhone1, ReferenceType2, ReferenceName2, ReferencePhone2, ReferenceEmail2, '
+ 'ReferenceType3, ReferenceName3, ReferencePhone3, ReferenceEmail3, DeptConditions, NoGuarantee, ServicePolicy, NoRefunds, PenaltyPerjury, Authorization) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '
+ '?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
stmt2.setString(2, new Date());
stmt2.setString(3, data.email);
stmt2.setString(4, data.section4);
stmt2.setString(5, data.section5);
stmt2.setString(6, data.section6);
stmt2.setString(7, data.section8);
stmt2.setString(8, data.pages1213);
stmt2.setString(9, data.minors);
stmt2.setString(10, data.storefirearm);
stmt2.setString(11, data.reftype1);
stmt2.setString(12, data.refcon1);
stmt2.setString(13, data.refphone1);
stmt2.setString(14, data.refemail1);
stmt2.setString(15, data.reftype2);
stmt2.setString(16, data.refcon2);
stmt2.setString(17, data.refphone2);
stmt2.setString(18, data.refemail2);
stmt2.setString(19, data.reftype3);
stmt2.setString(20, data.refcon3);
stmt2.setString(21, data.refemail3);
stmt2.setString(22, data.deptcondition);
stmt2.setString(23, data.review_agree1);
stmt2.setString(24, data.review_agree2);
stmt2.setString(25, data.review_agree3);
stmt2.setString(26, data.review_agree4);
stmt2.setString(27, data.review_agree5);
stmt2.setString(28, data.signature);
stmt2.execute();
I expect to add data to my SQL database.
If the column is auto_incremented, you do not need to provide a value for it. MySQL automatically assigns a new number when you perform an insert where no value is given for that column.
So you should just not specify this column in your insert, ie change:
var stmt2 = conn.prepareStatement(
'INSERT INTO tblPreInterview '
+ '(ID, Timestamp, Email, Section4, Section5, Section6, ...
To:
'INSERT INTO tblPreInterview '
+ '(Timestamp, Email, Section4, Section5, Section6, ...
Accordingly you should remove the first parameter.

Can't insert url in Mysql properly

I am using this code:
$image = mysqli_real_escape_string($dbc, $res[3][$i]);
Where $res[3][$i] is a url.
Then I store $image in a Mysql DB. But when I retrieve it, it's all messed up with the special characters... (my DB is in utf8).
How can I store a url in mysql and get it back exactly as it was?
Thanks
Actually I am using prepared statement:
$image = mysqli_real_escape_string($dbc, $res[3][$i]);
$md5_of_title = md5($title);
//$query = "INSERT IGNORE INTO scrap (date, journal, section, title, href, teaser, image, md5_of_title) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$query = "INSERT INTO scrap (date, journal, section, title, href, teaser, image, md5_of_title) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($dbc, $query);
/*i Integers;
d Doubles;
b Blobs;
s Everything Else;*/
mysqli_stmt_bind_param($stmt, "ssssssss", date('Y-m-d H:i:s'), $name, $section, $title, $href, $teaser, $image, $md5_of_title);
mysqli_stmt_execute($stmt);

Update Data in Mysql

My code is this
Profile.update_all(["is_active = ?, name = ?, geo_lat = ?, geo_long = ?, radius = ?, search_option = ?", 1, params[:profile_name],'null','null','null','st'])
but it assign
geo_lat=0
and
geo_long=0
In my database structure default value is null for both
and i want to assign null
please help me
The issue here are string 'null'.
Try the following:
Profile.update_all(["is_active = ?, name = ?, geo_lat = ?, geo_long = ?, radius = ?, search_option = ?", 1, params[:profile_name], nil, nil, nil, 'st'])
Also check your migration to see if default is not set to string 'null'.

Insert a row to MySQL table with jsp

I want to insert rows to mysql database with an jsp code, but I can´t.
Here is my jsp code without the html form:
<%
String login = request.getParameter("login");
String password = request.getParameter("password");
String full_name = request.getParameter("full_name");
String ulevel = request.getParameter("ulevel");
String team_id = request.getParameter("team_id");
Connection connection = null;
PreparedStatement pstatement = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
int updateQuery = 0;
if(login!=null && password!=null && full_name!=null && ulevel!=null && team_id!=null){
if(login!="" && password!="" && full_name!="" && ulevel!="" && team_id!="") {
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/android","root","root");
String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (null, ?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(2, login);
pstatement.setString(3, password);
pstatement.setString(4, full_name);
pstatement.setString(5, ulevel);
pstatement.setString(6, team_id);
updateQuery = pstatement.executeUpdate();
if (updateQuery != 0) { %>
When I press SUBMIT, the webpage shows me this:
type Status report message descriptionThe requested resource () is
not available.
p.s.: column user_id is set to autoincrement in mysql table, so I use null.
But I dont know, that´s the right way...
I run the code from netbeans 7.0.1
since ID is auto-incremented, get rid of it from the list, preparedstatement is looking for index 1 on its parameters.
String queryString = "INSERT INTO users(login,password,full_name,ulevel,team_id) VALUES (?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, login);
pstatement.setString(2, password);
pstatement.setString(3, full_name);
pstatement.setString(4, ulevel);
pstatement.setString(5, team_id);
this one below is not tested
just start you parameter with 1 because there are only 5 parameters to be defined.
String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (null, ?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, login);
pstatement.setString(2, password);
pstatement.setString(3, full_name);
pstatement.setString(4, ulevel);
pstatement.setString(5, team_id);
I found the problem. The error was in html form, at attribute action, where i had an source file, which i never had before. :/
String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (?, ?, ?, ?, ?)";
You should not use anything not even null for query!