Home > Back-end >  Use delect the from where to delete not to drop the database content
Use delect the from where to delete not to drop the database content

Time:12-25



Above is I want to delete the specified line database, why can't I speak according to the online method delete repository specified row! (my design is in the window input correct user name and password and then click the logout deleted the user account password)
Here is my code
Try
{
Class.forName("com.mysql.jdbc.Driver");
Conn=DriverManager. GetConnection (sql_url, name, password);//connect to the database
PreparedStatement ps=null;
ResultSet rs=null;
PreparedStatement PreparedStatement=null;
Int d=0;
DefaultTableModel model=(DefaultTableModel) table. The getModel ();
for (int i=0; I & lt; Model. GetRowCount (); I++)
{
Object value2=model. GetValueAt (I, 0).
System. The out. Println (value2);
The Object value3=model. GetValueAt (I, 1);
System. The out. Println (value3);
If (textField. GetText () equals (value2))
{

If (passwordField. GetText () equals (value3))
{
System.out.println(i);
String SQL="DELETE FROM programer WHERE UserName=value2";
//a preparedStatement=conn. ExecuteUpdate (" delete from programer where I ");
New prompt2 ();
Frame2. RemoveNotify ();
break;
}
The else
{
JOptionPane pane=new JOptionPane (" password error ");
JDialog dialog=pane. CreateDialog (" warning ");
Dialog. The show ();
}
}
The else
{
D++;
}
}
If (d>=model. GetRowCount ())
{
JOptionPane pane=new JOptionPane (" the user ");
JDialog dialog=pane. CreateDialog (" warning ");
Dialog. The show ();
}

}
The catch (a ClassNotFoundException e1)
{
//TODO Auto - generated the catch block,
E1. PrintStackTrace ();
{} the catch (SQLException e1)
//TODO Auto - generated the catch block,
E1. PrintStackTrace ();
}

CodePudding user response:

The
refer to the original poster SwartS response:


Above is I want to delete the specified line database, why can't I speak according to the online method delete repository specified row! (my design is in the window input correct user name and password and then click the logout deleted the user account password)
Here is my code


Learn to stick the code
I haven't seen you of SQL statement execution, of course not deleted,

CodePudding user response:

Well, I want to use Java is not in SQL operation, also want to open the SQL statement execution

CodePudding user response:

 

(1) the JDBC programming steps:
Step 1: load the database driver, usually use Class Class class.forname () static method to load the driver:
//the load driver
Class. ForNamed (DriverClass);
Among these is DriverClass database driven class corresponds to a string, for example, to load the MySQL driver code below
//load the MySQL driver
Class.forName("com.mysql.jdbc.Driver");

Step 2: database connection, captured by the DriverManager DriverManager provides the following methods:
//access database connection
DriverManager. GetConnection (String url, String user, String pass);

Step 3: create Statement object through the Connection object, there are three ways of Connection to create Statement:
* * createStatement () to create the basic Statement object
* * prepareStatement (String SQL) according to the incoming SQL Statement create precompiled Statement object
* * prepareCall (String SQL) according to the incoming object of SQL statements to create the CallableStatement

All use the Statement executing SQL statements, the Statement has the following three methods to execute SQL statements:
* * * * excute () : can perform any SQL statement, but more troublesome
* * * * excuteUpdate () is mainly used for DDL and DML statements, executing DML statement returns the number of rows affected by the SQL statement, DDL statement returns 0
* * * * excuteQuary () can only execute the query, execute returned ResultSet object represents the query results,

Step 5: operation result set
If you execute the SQL statement is the query, the execution results will return a ResultSet object, the object save run the SQL statement in the query results, the program can be removed by operating the ResultSet object query results, the ResultSet object provides the following two kinds of methods:
Previous * * next () () the first (last) beforeFirst () () afterLast absolute () () moves the pointer records of methods, such as
* * getXxx () method to get the record pointer pointing to the line of the particular column value, this method not only can use the column index as a parameter, you can also use the column name as a parameter, use the column index do better performance parameters, use the column names more readable,

Step 6: recycle database resources, including closing ResuleSet, Statement and Connection resources, such as


//use the DriverManager access to database connection, including the returned connection means amount of Java database connection
The Connection conn=DriverManager. GetConnection (" JDBC: mysql://localhost: 3306/mydb ", "root", "root");
//use the Connection to create a Statement object
The Statement STMT=conn. CreateStatement ();
//executing SQL statements

The ResultSet rs=STMT. ExecuteQuery (" select s. *, teacher_name "+" from student_table s, teacher_table t "
+ "where t.t eacher_id=s.j ava_teacher");
{
//the ResultSet have a series of getXxx () method, is used to retrieve the record pointer pointing to a particular line, the value of the column constant with the next () method will be the pointer moves to the next line, if the move after the record pointer
//points to the line effectively, the next () method returns true
While (rs), next ()) {
System. The out. Println (rs. Get int (1) + "\ t" + rs. Get string (2) + "\ t" + rs. Get string (3) + "\ t" + rs. Get string (4));

CodePudding user response:

refer to the second floor SwartS response:
well, I want to use Java is not in SQL operation, also want to open a SQL statement execution


In the program code didn't perform STMT. ExecuteUpdate (SQL),

CodePudding user response:

reference 4 floor icoolno1 response:
Quote: refer to the second floor SwartS response:
well, I want to use Java is not in SQL operation, also want to open a SQL statement execution


In the program code didn't perform STMT. ExecuteUpdate (SQL),
STMT how to define the Settings

CodePudding user response:

reference 1st floor qq_39936465 response:
Quote: refer to the original poster SwartS response:


Above is I want to delete the specified line database, why can't I speak according to the online method delete repository specified row! (my design is in the window input correct user name and password and then click the logout deleted the user account password)
nullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related