JDBC Programming Example

Source: Internet
Author: User
Tags driver manager

Tag: port date () date manage string concatenation for EXCE teacher Package

Package com.lovo.test;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.ArrayList;

Import Javax.swing.JOptionPane;

Import Com.lovo.bean.ClassBean;

public class Testdml {

public static void Main (string[] args) {
String className = Joptionpane.showinputdialog ("Please enter class name");
String teachername = Joptionpane.showinputdialog ("Please enter the class name");

Database operation steps:
1. Load driver---Tell the Driver Manager which database we will use to drive the package
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

2. Operation of JDBC API to complete database action
2-1. Get the connection
Connection con = null;
try {
url--Uniform Resource Locator----style: protocol://IP Address: Port number/Service
con = drivermanager.getconnection ("jdbc:mysql://127.0.0.1:3306/test134" +
"? Useunicode=true&characterencoding=utf8&usessl=false", "root", "root");
2-2. Write SQL statements---string concatenation,
String sql = "INSERT into T_class (f_classname,f_teacher) VALUES ('" +classname+ "', '" +teachername+ "')";
2-3. Get the Statement object---statement object
Statement state = Con.createstatement ();
2-4, executes the statement object---All DML statements, all execute the executeupdate () method
int row = state.executeupdate (sql);//The int returned represents how many rows were affected!
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally{
2-5. Close the connection
if (con! = null) {
try {
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}










}
}

JDBC Programming Example

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.