在使用jdbc将mysql中的数据查询出来更新到sqlserve数据库时,怎么自动转换类型JDBC操作MySQL数据库的步骤1、准备MySQL数据库驱动包:mysql-connector-java-5.0.8-bin.jar,一个项目中只能存在一个版本的驱动包a、复制该驱动包,粘贴到项目中b、选中项目里的驱动包,右键->Build Path->Add to Build Path2、在类里写代码加载驱:决定连接哪种数据库a、Class.forName("com.mysql.jdbc.Driver");b、必须进行异常处理:ClassNotFoundException3、连接数据库a、Connection con=DriverManager.getConnection("连接字符串", "用户名", "密码");b、连接字符串格式固定,不同数据库,格式不同:jdbc:mysql://要连接的计算机名称:端口号/要连接的数据库名称c、必须进行异常处理:SQLException4、拼写要执行的sql语句,必须是可以在数据库中执行的5、创建执行sql语句的对象a、Statement stmt=con.createStatement();b、注意:Statement必须来自于java.sql包中6、执行sql语句a、执行insert、update和delete语句:int row=stmt.executeUpdate(sql);返回影响行数b、执行查询语句:ResultSet rs=stmt.executeQuery(sql);返回查询结果c、执行任意sql语句(DDL、DCL、DML和DQL等)7、对执行结果进行处理a、执行更新语句:判断影响行数是否为0,0表示失败,非0表示成功b、对查询结果进行处理:1) 结果集需要先移动、后取值 :rs.next();int id=rs.getInt(1);String name=rs.getString("loginName");2) 结果集有多条时,需要循环操作:while(rs.next()){ System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4)+"\t"+rs.getInt(5));}3) 不确定是否有查询结果时:if(rs.next()){说明有查询结果}else{没有查询结果}4) 使用了聚合函数,一定有查询结果,查询结果是一行一列:rs.next();int result=rs.getInt(1);注意:结果集取值时取出的时查询语句中包含的字段,与表中字段无关9、关闭相关对象(先关闭结果集对象、在关闭执行语句对象,最后关闭连接对象)例如:执行查询语句Scanner input=new Scanner(System.in);System.out.print("请输入登录名: ");String name=input.next();System.out.print("请输入密码: ");String pass=input.next();try {Class.forName("com.mysql.jdbc.Driver");Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "root");String sql="select COUNT(*) from UserInfo where loginName='"+name+"' and loginPass='"+pass+"'";Statement stmt=con.createStatement();ResultSet rs=stmt.executeQuery(sql);rs.next();int result=rs.getInt(1);if(result!=0){System.out.println("登录成功!");}else{System.out.println("用户名或密码错误,请重新登录!");}rs.close();stmt.close();con.close();} catch (ClassNotFoundException e) {System.out.println("加载驱动错误:"+e.getMessage());} catch (SQLException e) {System.out.println("数据库操作错误:"+e.getMessage());}执行添加、修改和删除语句try {//加载驱动Class.forName("com.mysql.jdbc.Driver");//连接数据库Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "root");//拼写要执行的sql语句String sql="update UserInfo set loginPass='111' where loginName='a'";//String sql="insert UserInfo values(default,'test','test')";//String sql="delete from UserInfo where loginName='a'";//创建执行语句对象Statement stmt=con.createStatement();//执行int row=stmt.executeUpdate(sql);//处理结果if(row==0){System.out.println("修改失败!");}else{System.out.println("修改成功!");}//关闭stmt.close();con.close();} catch (ClassNotFoundException e) {System.out.println("驱动加载错误:"+e.getMessage());} catch (SQLException e) {System.out.println("数据库操作错误:"+e.getMessage());}