Connect java web to mysql and javamysql

Source: Internet
Author: User

Connect java web to mysql and javamysql

 

1. Create a database

1 create database jdbc;

 

2. Create a data table

1 create table create table stu10( id int(10) primary key auto_increment, name varchar(20) not null, age varchar(10) not null, address varchar(20) not null);

 

3. Add data using the insert statement

1 insert into stu10 values (1, 'code-Nu', '20', 'jiaxing, Zhejiang ');

4. Create a database connection class

1 package com. tu. jdbc; 2 3 import java. SQL. connection; 4 import java. SQL. driverManager; 5 import java. SQL. SQLException; 6 7 public class Jdbc_con {8 public Connection getConnection () {9 try {10 // load the database driver 11 Class. forName ("com. mysql. jdbc. driver "); 12 // connect to the database 13 return DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/jdbc", "root", "123"); 14} catch (Exception e) {15 // TODO Auto-generated catch Block16 e. printStackTrace (); 17} 18 return null; 19} 20 21 public void closeConection (Connection con) {22 if (con! = Null) {23 try {24 con. close (); 25} catch (SQLException e) {26 // TODO Auto-generated catch block27 e. printStackTrace (); 28} 29} 30} 31}

5. Create a test class

 1 package com.tu.jdbc; 2  3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7  8 public class Test { 9 10     public static void main(String[] args) {11         Test ts = new Test();12         ts.list();13 14     }15 16     public void list() {17         Jdbc_con jc = new Jdbc_con();18         Connection jdbc = jc.getConnection();19         String sql = "select id,name from stu10";20         try {21             Statement sm = jdbc.createStatement();22             ResultSet rs = sm.executeQuery(sql);23             while (rs.next()) {24                 int id = rs.getInt(1);25                 String name = rs.getString(2);26                 System.out.println(id + name);27             }28         } catch (SQLException e) {29             // TODO Auto-generated catch block30             e.printStackTrace();31         }32 33     }34 }

 

Related Article

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.