Insert using connector j?
Posted by: Branden Patey
Date: July 07, 2014 01:48PM

I'm trying to insert a a row into a mysaql database containing a number ID,name,email and password using connector J. It's not working and I'm not sure if it's how I'm using connector J but this is my first guess.

the variables are retrieved from text boxes in a Swing GUI using this code:

@Override
public void actionPerformed(ActionEvent e) {
List<String> user_array = new ArrayList<String>();
if (!ID.getText().trim().equals("")){
user_array.add(ID.getText().trim());
}
if (!email.getText().trim().equals("")){
user_array.add(email.getText().trim());
}
if (!pass.getText().trim().equals("")){
user_array.add(pass.getText().trim());
}
if (!name.getText().trim().equals("")){
user_array.add(name.getText().trim());
}
if(!user_array.isEmpty())
{
String[] user = user_array.toArray(new String[5]);
Lottery.employees.addEmployeeMysql(user);
}
}
});
btnadd.setBounds(404, 222, 56, 23);
panel.add(btnadd);

~!~and the addEmployeeMysql method:~!~


public static void addEmployeeMysql(String[] user) {
String[] xsettings = null;
try {
xsettings = Lottery.properties.readConfig();
} catch (ParserConfigurationException | SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://"+ xsettings[0] +"/"+ xsettings[4] +"?user="+ xsettings[2] +"&password="+ xsettings[3]);
stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO employees (ID,name,email,password) VALUES ("+ user[0] +","+ user[1] +","+ user[2] +","+ user[3] +")");
} catch (Exception ex) {
// handle the error
}finally {

if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore

rs = null;
}

if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore

stmt = null;
}
}
}

Options: ReplyQuote


Subject
Written By
Posted
Insert using connector j?
July 07, 2014 01:48PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.