不太好意思,你的问题我没太看明白,给你个jdbc操作mysql数据库实例代码参考一下,希望能对你有所帮助- package com.ty.test;
- import java.sql.*;
- public class TestMysql {
-
- //测试如何操作mysql数据库
- public static void main(String args[])
- {
- Connection ct=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- //1.加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- //2.得到链接
- ct=DriverManager.getConnection
- ("jdbc:mysql://localhost:3306/spdb1?useUnicode=true&characterEncoding=UTF-8","root","数据库的密码");
- //3.创建ps<ps代表预编译的sql对象>
- ps=ct.prepareStatement("select * from test14");
- //4.执行
- rs=ps.executeQuery();
- while(rs.next())
- {
- System.out.println("姓名="+rs.getString("name")+" 薪水="+rs.getFloat("sal"));
- }
- } catch (Exception e) {
- // TODO: handle exception
- }finally{
- //关闭
- if(rs!=null)
- {
- try {
- rs.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- rs=null;
- }
- if(ps!=null)
- {
- try {
- ps.close();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ps=null;
- }
- if(ct!=null)
- {
- try {
- ct.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ct=null;
- }
- }
- }
- }
复制代码 |