Insert SQL code in JAVA, insert SQL in java
Insert data import java. SQL. *;/*** @ version 2012-02-22 * @ author */public class InsertDemo {public static void main (String [] args) throws SQLException {Connection conn = null; statement stmt = null; String JDriver = "com. microsoft. sqlserver. jdbc. SQLServerDriver ";//
String connectDB = "jdbc: sqlserver: // localhost: 1433; DatabaseName = JDBCTest ";//
String user = "sa"; String pwd = "SQL"; try {// Driver Class for Dynamic Database Import. forName (JDriver); // obtain the database link conn = DriverManager. getConnection (connectDB, user, pwd); // create the SQL statement String SQL = "INSERT INTO JDBCTestTable (user_name, user_password) VALUES ('balabala ', '123 ')"; // execute the SQL statement stmt = conn. createStatement (); stmt.exe cuteUpdate (SQL); System. out. println ("data inserted successfully");} catch (Exception e) {e. printStackTrace (); stmt. close (); conn. close () ;}} modify the data import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. statement; public class UpdateDemo {public static void main (String [] args) throws SQLException {Connection conn = null; Statement stmt = null; try {// Driver Class for Dynamic Database Import. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver "); // obtain the database link conn = DriverManager. getConnection ("jdbc: sqlserver: // localhost: 1433; DatabaseName = JDBCTest", "sa", "SQL "); // create an SQL statement String SQL = "UPDATE JDBCTestTable SET user_password = '000000' WHERE user_name = 'Eric '"; // execute the SQL statement stmt = conn. createStatement (); stmt.exe cuteUpdate (SQL); System. out. println ("data updated successfully");} catch (Exception e) {e. printStackTrace (); stmt. close (); conn. close () ;}} delete data import java. SQL. connection; import java. SQL. driverManager; import java. SQL. statement; public class DeleteDemo {public static void main (String [] args) {String JDriver = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; String url =" jdbc: sqlserver: // 127.0.0.1: 1433; DatabaseName = JDBCTest "; String user =" sa "; String password =" SQL "; try {Class. forName (JDriver); // The Connection conn = DriverManager driver for Dynamic Database Import. getConnection (url, user, password); // obtain the database link String query = "delete from JDBCTestTable WHERE user_name = 'Eric '"; // create an SQL Statement stmt = conn. createStatement (); // execute the SQL statement stmt.exe cuteUpdate (query); System. out. println ("data deleted"); stmt. close (); conn. close ();} catch (Exception e) {e. printStackTrace () ;}} queries data import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. statement; public class QueryDemo {public static void main (String [] args) {String JDriver = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; String url =" jdbc: sqlserver: // 127.0.0.1: 1433; DatabaseName = JDBCTest "; String user =" sa "; String password =" SQL "; try {Class. forName (JDriver); // The Connection conn = DriverManager driver for Dynamic Database Import. getConnection (url, user, password); // obtain the database link String query = "SELECT * FROM JDBCTestTable"; // create an SQL Statement stmt = conn. createStatement (); // execute the SQL statement ResultSet rs1_stmt.exe cuteQuery (query); while (rs. next () {System. out. println (rs. getString ("user_name") + ":" + rs. getString (2); // The password field number starts from 1, and the password is ranked second} System. out. println ("Data queried successfully"); rs. close (); stmt. close (); conn. close ();} catch (Exception e) {e. printStackTrace ();}}}