Simple Java connection to the database

Source: Internet
Author: User

[Java] import java. SQL. *; public class ConnDatabase {private static final String DBDRIVER = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; private static final String DBURL =" jdbc: sqlserver: // localhost: 1433; DatabaseName = School1 "; private static final String DBUSER =" sa "; private static final String DBPWD = "sa"; // Refresh public static void Query (ResultSet rs) throws Exception {System. out. println ("No \ t Name \ tAge \ tSex "); // student ID, Name, age, gender while (rs. next () {String no = rs. getString ("No"); String name = rs. getString ("Name"); int age = rs. getInt ("Age"); String sex = rs. getString ("Sex"); System. out. println (no + "\ t" + name + "\ t" + age + "\ t" + sex);} public static void main (String [] args) throws Exception {Class. forName (DBDRIVER); // load the driver Connection conn = DriverManager. getConnection (DBURL, DBUSER, DBPWD); // connect to the database Sta Tement stmt = conn. createStatement (); // database operation // CREATE a TABLE stmt.exe cute ("create table Stu5" + "(No varchar (20) primary key," + "Name varchar (10 ), "+" Age int, "+" Sex varchar (2) "); String sqlResult =" select * from Stu5 "; ResultSet rs = stmt.exe cuteQuery (sqlResult ); query (rs); // Add stmt.exe cuteUpdate ("insert into Stu5 (No, Name, Age, Sex)" + "VALUES ('20170101', 'day', 19, 'femal') "); rs = stmt.exe cuteQuery (sqlResult); Query (rs );/ /Change stmt.exe cuteUpdate ("UPDATE Stu5 SET Age = 20 WHERE Name = 'chen Yan'"); rs = stmt.exe cuteQuery (sqlResult); Query (rs ); // Delete stmt.exe cuteUpdate ("delete from Stu5 WHERE Name = 'chen Yan'"); rs = stmt.exe cuteQuery (sqlResult); Query (rs); rs. close (); stmt. close (); conn. close () ;}} PS: private static final String DBURL = "jdbc: sqlserver: // localhost: 1433; DatabaseName = School1"; this line of code has two points to note: 1. DBURL needs to be changed according to the actual situation. 2. DatabaseName = School1. Previously I wrote DatabaseName = 'school1 ', and the result was wrong. It took me a long time to find out and correct this small mistake. I hope you will not make the same mistake as me.

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.