Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
public class MyConnection {
static Statement Statement = null;
static PreparedStatement Prestat = null;
static ResultSet ResultSet = null;
static Connection con = null;
public static void Main (string[] args) {
MyConnection mycon = new MyConnection ();
Connection con = mycon.getconnection ();
Data traversal
statement used to execute SQL statements
try {
statement = Con.createstatement ();
The SQL statement to execute
String sql = "SELECT * from Student";
Result set
ResultSet = statement.executequery (sql);
Traversing data
while (Resultset.next ()) {
String Sid = resultset.getstring (1);
String Sname = resultset.getstring (2);
String Sage = resultset.getstring (3);
String Ssex = resultset.getstring (4);
String szhuanye = resultset.getstring (5);
System.out.println ("SID:" + Sid + "Sname:" + Sname
+ Sage: + Sage + "Ssex:" + Ssex + "Szhuanye:"
+ Szhuanye);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Perform additions and deletions and change the string of stitching *********
INSERT INTO student valuse (' "+sid+" ', ' "+sname+" ' ...);
Pre-processing and deletion and modification **************
String insert = "INSERT into student values (?,?,?,?,?)";
String update = "Update student set sname=?" where sid=? ";
String Delete = "Delete from student where sid=?";
Add data
try {
Prestat = con.preparestatement (insert);
You might want to check the database constraints here.
Prestat.setstring (1, "100080");
Prestat.setstring (2, "I Try");
Prestat.setstring (3, "100");
Prestat.setstring (4, "male");
Prestat.setstring (5, "100080");
Execute SQL statement
Prestat.executeupdate ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Update data
try {
Prestat = con.preparestatement (update);
Prestat.setstring (1, "change");
Prestat.setstring (2, "100080");
Prestat.executeupdate ();
} catch (SQLException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
Delete data
try {
Prestat = con.preparestatement (delete);
Prestat.setstring (1, "100080");
Prestat.executeupdate ();
} catch (SQLException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
Preprocessing statements PreparedStatement querying data
try {
Prestat = Con.preparestatement ("select * from Student where sid=?");
Setting a parameter indicates that the first parameter is 100002
Prestat.setstring (1, "100080");
Executing a preprocessing statement
ResultSet = Prestat.executequery ();
Traversing data
while (Resultset.next ()) {
String Sid = resultset.getstring (1);
String Sname = resultset.getstring (2);
String Sage = resultset.getstring (3);
String Ssex = resultset.getstring (4);
String szhuanye = resultset.getstring (5);
System.out.println ("SID:" + Sid + "Sname:" + Sname
+ Sage: + Sage + "Ssex:" + Ssex + "Szhuanye:"
+ Szhuanye);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Database connection Methods
Public Connection getconnection () {
Driver name
String Driver = "Com.mysql.jdbc.Driver";
The URL points to the database name you want to access score
String url = "Jdbc:mysql://localhost:3306/score?useunicode=true&characterencoding=utf-8";
Note score the code behind the home is the specified encoding does not know that there may be garbled
String url= "Jdbc:mysql://127.0.0.1:3306/score";
User name when MySQL is configured
String user = "root";
Password for MySQL configuration
String password = "123456";
try {
Load driver loads a class's source data object into memory by name
Class.forName (driver);
Continuous database
con = drivermanager.getconnection (url, user, password);
if (!con.isclosed ())
SYSTEM.OUT.PRINTLN ("Database connection succeeded! ");
} catch (Exception e) {
E.printstacktrace ();
}
return con;
}
}