简单用JDBC与mysql数据库进行连接

来源:互联网 发布:德尔康尼骨科知乎 编辑:程序博客网 时间:2024/06/14 16:59

1.创建一个静态方法Connection用于进行与数据库的连接

(部分重要代码有注释)

//与数据库建立连接的方法public static Connection getConnection(){//构造一个连接connConnection conn=null;/* * 建立连接所需要的数据,ip地址,用户名和密码 * jdbc是java与数据库连接的组件 * mysal是数据库的类型,还有其他数据库类型(如sql、oracle) * localhost:3306是ip地址+3306端口号 * introduce是数据库的名字*/String ip="jdbc:mysql://localhost:3306/introduce?useSSL=false";String username="root";String password="123456";try{//加载这个driver类,而前面的是这个类所在的包名Class.forName("com.mysql.jdbc.Driver");//建立连接与数据库conn=DriverManager.getConnection(ip,username,password);}catch(ClassNotFoundException e){e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}

2.从数据库中获取用户信息

//获取数据库里面id=1的用户的信息public static void getUser(Connection conn){//声明,声明程序需要执行的内容Statement stmt=null;//结果集,用户保存声明后所获取的东西ResultSet rs=null;try {//对连接数据库这件事进行实例化,用于操作数据库stmt =conn.createStatement();//保存这条语句执行的结果,executeQuery+数据库语句rs=stmt.executeQuery("select * from user where id=1");//将指针指向集合内部的数据,没有这句话不能访问访问rs内的数据rs.next();int id=rs.getInt("id");String username=rs.getString("username");String pic=rs.getString("pic");String introduce=rs.getString("introduce");String detail_intro=rs.getString("detail_intro");String href_url=rs.getString("href_url");String intro1=rs.getString("intro1");String photo1=rs.getString("photo1");String intro2=rs.getString("intro2");String photo2=rs.getString("photo2");String intro3=rs.getString("intro3");String photo3=rs.getString("photo3");System.out.println("id="+id);System.out.println("username="+username);System.out.println("pic="+pic);System.out.println("introduce="+introduce);System.out.println("detail_intro="+detail_intro);System.out.println("href_url="+href_url);System.out.println("intro1="+intro1);System.out.println("photo1="+photo1);System.out.println("intro2="+intro2);System.out.println("photo1="+photo1);System.out.println("intro3="+intro3);System.out.println("photo3="+photo3);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try{//运行完后要关闭这些集合,以免数据泄露rs.close();stmt.close();conn.close();}catch(SQLException e){e.printStackTrace();}}}


3.通过与数据库连接获取用户对象

(首先你要新建一个User类来保存用户对象,这里省略这个步骤)

//获取一个id=userId的Userpublic static User getUserById(int userId){User user=new User();Connection conn=getConnection();Statement stmt=null;ResultSet rs=null;try {stmt =conn.createStatement();//把id=userId的user的信息全部存到rsrs=stmt.executeQuery("select * from user where id="+userId);rs.next();//用变量保存rs内的信息int id=rs.getInt("id");String username=rs.getString("username");String pic=rs.getString("pic");String introduce=rs.getString("introduce");String detail_intro=rs.getString("detail_intro");String href_url=rs.getString("href_url");String intro1=rs.getString("intro1");String photo1=rs.getString("photo1");String intro2=rs.getString("intro2");String photo2=rs.getString("photo2");String intro3=rs.getString("intro3");String photo3=rs.getString("photo3");//把上面的信息设置到一个新的user里,在最后返回这个user对象user.setId(id);user.setUsername(username);user.setPic(pic);user.setIntroduce(introduce);user.setDetail_intro(detail_intro);user.setHref_url(href_url);user.setIntro1(intro1);user.setPhoto1(photo1);user.setIntro2(intro2);user.setPhoto2(photo2);user.setIntro3(intro3);user.setPhoto3(photo3);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try{//运行完后要关闭这些集合,以免数据泄露rs.close();stmt.close();conn.close();}catch(SQLException e){e.printStackTrace();}}return user;}


测试代码:
public static void main(String[] args) {// TODO Auto-generated method stubConnection conn=DBUser.getConnection();getUser(conn);//获取id=1的User对象User u=getUserById(1);System.out.println(u.getId());System.out.println(u.getUsername());}
方法1结果:


方法2结果:


数据库User表的信息:


成功连接数据库。

4.Navicat for MySQL

一款用于MySQL数据库进行图形化界面操作的软件,让用户更简洁的操作数据库。

0 0