elcipse与tomcat下载与安装:https://www.cnblogs.com/duzhuo/p/5425834.html
Demo01:数据库连接
package com.bjsxt.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Demo01 { public static void main(String[] args){ //loading 加载驱动类 //第一步 try{ Class.forName("com.mysql.jdbc.Driver"); //建立连接 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjsbc","****","****"); System.out.print(conn); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
demo02:数据库查询
package com.bjsxt.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Demo02 { public static void main(String[] args){ //loading 加载驱动类 //第一步 try{ Class.forName("com.mysql.jdbc.Driver"); //建立连接 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjsbc","***","****"); // Statement stmt=conn.createStatement(); // String sql="insert into t_user values('2','xiaotian','36')"; // stmt.execute(sql); //测试sql注入 where id=5 or 1=1 这样数据库没保障 故statment类不安全,接下来用preparetstatement /*String sql='delete from t_user where id=5'; stmt.execute(sql);*/ }catch(ClassNotFoundException e){ e.printStackTrace(); }catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
批处理: