CSDN博客地址

java---数据库操作

连接数据库的代码:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class main{

            public static void main(String[]args){

                     Connnection con;//声明Connection对象

                     String driver="com.mysql.jdbc.Driver";//驱动程序名

                     String url="jdbc:mysql://localhost:3306/sqltestdb";//url指向要访问的数据库名的mydata

                     String  user="root";

                     String password="123456";

                     //遍历查询结果集

                     try{

                            Class.forName(driver);

                            //getConnection方法连接数据库

                            con=DriverManager.getConnection(url,user,password);

                            if(!con.isClosed())

                                    System.out.println("Succeeded  connecting to the database");

                          //创建Statement对象来执行sql语句

                           Statement statement=con.createStatement();

                           String sql="select * from emp";// 要执行的sql语句

                           ResultSet rs=statement.excuteQuery(sql);   //ResultSet类用来存放结果集

                           System.out.println("--------------");

                           System.out.println("执行结果如下:");

                           System.out.println("---------------");

                           System.out.println("姓名"+"\t"+"职称");

                           System.out.println("--------------");

                           String job=null;

                           String id=null;

                            while(rs.next()){

                              //获取stuname这列数据

                              job=rs.getString("job");

                              id=rs.getString("ename"); 

                              System.out.println(job+"\t"+id);

                                }

                             rs.close();

                             con.close();

                            }catch(ClassNotFoundException e){

                                 System.out.println("sorry,i can't find driver");

                                 e.printStackTrace();

                                 }catch(SQLException e){

                                        e.printStacktrace();

                                            }catch(Exception e){

                                             e.printStackTrace();

                                             }

                                   finally{

                                    System.out.println("数据库成功获取到数据");

                                          }

                 }

}

          

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import  java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import org.user.entity.User;

import org.user.util.DBUtil;     //从其他包中获取的对象

public class UserDo{

//根据用户名查找用户

public User findUserByName(String UserName){

                            //编写sql语句

String sql="select * from User_Info where UserName=?";

//获得连接

Connection conn=DBUTil.getConnection();

  //有返回的结果

ResultSet rs=null;

//实例化一个user对象

User user=new User();

try{

//用来发送sql语句

PreparedStatement ps=conn.preparedStatement(sql);

//设置要输入的参数

ps.setString(1,userName);

//执行sql语句

ps.executeQuery();

//如果能找到结果

if(rs.next()){

//则把结果一步一步的set到User对象中

user.setUid(rs.getInt(1));

user.setUserName(rs.getString(2));

user.setUserPss(rs.getString(3));

}

}catch(SQLException e){

e.printStackTrace();

}

finally{

//关闭连接

DBUtil.closeConn(conn,null,rs);

}

//最后要把user返回去

return user;

}

//添加用户
    //在这里把整个用户对象传进来
    public int addUser(User user){
        //编写sql语句
        String sql = "insert into USER_INFO values(?,?,?)";
        Connection conn= DBUtil.getConnection();
        //有返回的结果
        ResultSet rs= null;
        int i=0;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            //这些参数是要保存到数据的
            ps.setInt(1, user.getUid());
            ps.setString(2, user.getUserName());
            ps.setString(3, user.getUserPass());
            //执行sql语句
            i=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.closeConn(conn, null, rs);
        }
    return i;
    }
    public static void main(String[] args) {
        UserDao dao = new UserDao();
        User i = dao.findUserByName("zhang");
        System.out.println(i.getUserPass());
        
    }
}
 

 

                                                  

posted @ 2018-01-13 16:54  Yi_warmth  阅读(116)  评论(0编辑  收藏  举报
CSDN博客地址