Insert SQL code in JAVA, insert SQL in java

Source: Internet
Author: User

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 ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.