jdbc连接mysql数据库
//连接mysql数据库的步骤
//1.
加载驱动
//2.用drivermanager获得数据库连接
//3.实例化QueryRunner
//4.利用qr.update(),实现增删改
//5.利用qr.query()得到结果集
1 public class UtilMysql { 2 //连接mysql数据库的步骤 3 //1. 加载驱动 4 //2.用drivermanager获得数据库连接 5 //3.实例化QueryRunner 6 //4.利用qr.update(),实现增删改 7 //5.利用qr.query()得到结果集 8 private static String driverClassName="com.mysql.jdbc.Driver"; 9 private static String userName="root"; 10 private static String pwd=""; 11 private static String url="jdbc:mysql://127.0.0.1:3306/mydata?useUnicode=true&characterEncoding=utf8"; 12 13 //加载驱动,获取数据库连接 14 public static Connection getConnection() 15 { 16 Connection conn=null; 17 try { 18 Class.forName(driverClassName); 19 conn= DriverManager.getConnection(url,userName,pwd); 20 } catch (ClassNotFoundException e) { 21 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 22 } catch (SQLException e) { 23 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 24 } 25 return conn; 26 } 27 28 29 }
2.实例类
public class PeopleBean { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
3.数据库的增删改查
import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Map; /** * Created with IntelliJ IDEA. * User: kinkoo * Date: 14-3-11 * Time: 上午11:08 * To change this template use File | Settings | File Templates. */ public class MysqlDemo { //插入语句 public static void insert_sql() throws SQLException { //获得数据库连接 Connection con=UtilMysql.getConnection(); //获取QueryRunner对象 QueryRunner qrunner=new QueryRunner(); //执行sql语句 int n=qrunner.update(con,"insert into people(pname,age) values(‘bb‘,13)"); System.out.println("成功插入"+n+"条数据"); //关闭数据库 DbUtils.closeQuietly(con); } //删除 public static void delete_sql() throws SQLException { Connection con=UtilMysql.getConnection(); QueryRunner qrunner=new QueryRunner(); int n=qrunner.update(con,"delete from people where id=7"); System.out.println("成功删除"+n+"条数据"); DbUtils.closeQuietly(con); } //更新 public static void update_sql() throws SQLException { Connection con=UtilMysql.getConnection(); QueryRunner qrunner=new QueryRunner(); int n=qrunner.update(con,"update people set pname=‘pp‘ where id=4"); System.out.println("成功更新"+n+"条数据"); DbUtils.closeQuietly(con); } //查询 public static void select_sql() throws SQLException { Connection con=UtilMysql.getConnection(); QueryRunner qrunner=new QueryRunner(); List<Map> list=(List)qrunner.query(con,"select * from people",new MapListHandler()); //输出结果集 for(Map m:list) { System.out.println(m); } DbUtils.closeQuietly(con); } public static void main(String args[]) throws SQLException { insert_sql(); delete_sql();; update_sql(); select_sql(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。