1.java connecting MySQL Database
Code area:
Package Com.oracle.jdbc.demo1;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.sqlexception;public class Jdbcdemo {//four properties (four constant strings)/*jdbcnameurluserpassword*/private static final string Jdbcname= "Com.mysql.jdbc.Driver";p rivate static final String url= "jdbc:mysql://127.0.0.1:3306/emp_dept";p rivate Static final string user= "root";p rivate static final string password= "123456";/* * A Class (Drivermaneger) four interfaces (Connection,) * */public static void Main (string[] args) {//TODO auto-generated method stubconnection conn=null;try {class.forname (JDBCN AME); conn=drivermanager.getconnection (URL, user, password);//Get Conn to get the connection System.out.println ("Connection database Success");} catch (Exception e) {e.printstacktrace ();} finally {try {conn.close ();} catch (SQLException e) {//TODO auto-generated Cat CH Blocke.printstacktrace ();}}}
2. Adding data to a database in Java
The first method: Add data
Code area:
Package Com.oracle.jdbc.demo2;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.sqlexception;public class Jdbcadd {private static final String jdbcname= " Com.mysql.jdbc.Driver ";p rivate static final String url=" jdbc:mysql://127.0.0.1:3306/emp_dept ";p rivate static final String user= "root";p rivate static final String password= "123456";/* * A Class (Drivermaneger) four interfaces (Connection, PreparedStatement,) * */public static void Main (string[] args) {//TODO auto-generated method Stubconnection Conn=null;try {Class.forName (jdbcname); conn=drivermanager.getconnection (URL, user, password);//Add data to the operation string name= "Tianyu"; String sex= "female"; String sql= "INSERT into person values (null, '" "+name+" ', ' "+sex+" ') "; PreparedStatement pst=conn.preparestatement (SQL); Prepare to execute SQL statement int i=pst.executeupdate (); Returns the number of rows that successfully inserted data System.out.println ("+i+" record was added successfully); catch (Exception e) {e.printstacktrace ();} finally {try {conn.close ();} catch (SQLException e) {//TODO auto-generated Cat CH Blocke.printstacktrace ();}}}
The second method: adding data
Code area:
Package Com.oracle.jdbc.demo2;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.sqlexception;public class JDBCAdd2 {private static final String jdbcname= " Com.mysql.jdbc.Driver ";p rivate static final String url=" jdbc:mysql://127.0.0.1:3306/emp_dept ";p rivate static final String user= "root";p rivate static final String password= "123456";/* * A Class (Drivermaneger) four interfaces (Connection, PreparedStatement,) * */public static void Main (string[] args) {//TODO auto-generated method Stubconnection Conn=null;try {Class.forName (jdbcname); conn=drivermanager.getconnection (URL, user, password);//Add data to the operation string Name= "Tianyu 2"; String sex= "female"; String sql= "INSERT into person values (null,?,?)"; PreparedStatement pst=conn.preparestatement (SQL); Prepare to execute SQL statement pst.setstring (1, name); Fill in the 1th Hello pst.setstring (2, sex); Fill in the 2nd Hello int i=pst.executeupdate (); Returns the number of rows that successfully inserted data System.out.println ("+i+" record was added successfully); catch (Exception e) {e.printstacktrace ();} finally {try {conn.close ();} CAtch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
3. Modifying the contents of a database in Java
Code area:
Package Com.oracle.jdbc.demo3;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.sqlexception;public class Jdbcmodify {private static final String jdbcname= "Com.mysql.jdbc.Driver";p rivate static final String url= "jdbc:mysql://127.0.0.1:3306/emp_dept";p rivate static final String user= "root";p rivate static final String password= "123456";/* * A Class (Drivermaneger) four interfaces (Connection, PreparedStatement,) * */public static void Main (string[] args) {//TODO auto-generated method Stubconnection Conn=null;try {Class.forName (jdbcname); conn=drivermanager.getconnection (URL, user, password);//Modify the operation of the data int id=2; String name= "Wang Xibao"; String sex= "Male"; String sql= "Update person set name=?,sex=?" where id=? "; PreparedStatement pst=conn.preparestatement (SQL); Prepare to execute SQL statement pst.setstring (1, name); Fill in the 1th Hello pst.setstring (2, sex); Fill in the 2nd greeting Pst.setint (3, id); int i=pst.executeupdate (); Returns the number of rows that successfully modified the data System.out.println ("successfully modified" +i+ "Record");} catch (Exception e) {E.printstacktrace();} Finally {try {conn.close ()} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
4. Delete the contents of the database in Java
Code Area:
Package Com.oracle.jdbc.demo4;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.sqlexception;public class Jdbcdel {private static final String jdbcname= " Com.mysql.jdbc.Driver ";p rivate static final String url=" jdbc:mysql://127.0.0.1:3306/emp_dept ";p rivate static final String user= "root";p rivate static final String password= "123456";/* * A Class (Drivermaneger) four interfaces (Connection, PreparedStatement,) * */public static void Main (string[] args) {//TODO auto-generated method Stubconnection Conn=null;try {Class.forName (jdbcname); conn=drivermanager.getconnection (URL, user, password);//operation to delete data int id=2; String sql= "Delete from person where id=?"; PreparedStatement pst=conn.preparestatement (SQL); Prepare to execute SQL statement Pst.setint (1, id); int i=pst.executeupdate (); Returns the number of rows that successfully deleted data System.out.println ("successfully deleted" +i+ "Record");} catch (Exception e) {e.printstacktrace ();} finally {try {conn.close ();} catch (SQLException e) {//TODO auto-generated Cat CH Blocke.printstacktrace ();}}}}
5. View the contents of a database in Java
Code area:
Package Com.oracle.jdbc.demo5;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;public class JDBCFindAll {private Static final string Jdbcname= "Com.mysql.jdbc.Driver";p rivate static final string url= "Jdbc:mysql://127.0.0.1:3306/emp _dept ";p rivate static final string user=" root ";p rivate static final string password=" 123456 ";/* A class (Drivermaneger) of four interfaces (Connection, PreparedStatement, ResultSet,) * */public static void Main (string[] args) {//TODO Auto-generated method Stubconnection Conn=null;try {class.forname (jdbcname); conn=drivermanager.getconnection (URL, user, password);//query data operation string sql= "select Id,name,sex from"; PreparedStatement pst=conn.preparestatement (SQL); Prepare to execute SQL statement resultset rs=pst.executequery (); while (Rs.next ()) {int id=rs.getint ("id"); String name=rs.getstring ("name"); String sex=rs.getstring ("sex"); System.out.println (id+ "" +name+ "" +sex);}} catch (Exception e) {E.printstacktraCE ();} Finally {try {conn.close ()} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Adding and pruning Databases in Java