zoukankan      html  css  js  c++  java
  • mysql_jdbc

    package com.lovo.day18_jdbc1;


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;


    public class TestMain {


    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    //获取连接--通用代码
    Connection con = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");         //载入驱动类
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名",         
    "账号", "password");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    //新增记录
    //构造sql语句
    String sql = "insert into t_user(f_username,f_password,f_account) values(?,?,?

    )";
    System.out.println(sql);
    try {
    //获取预编译语句对象
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setString(1, "zhang6");
    ps.setString(2, "123456");
    ps.setDouble(3, 16000);
    //运行sql
    int row = ps.executeUpdate();
    System.out.println(row);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    if(con != null){
    try {
    con.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    //查询语句---查询单条记录
    // UserInfo theUser = null;
    // String inputName = JOptionPane.showInputDialog("请输入username:");
    // String inputPwd = JOptionPane.showInputDialog("请输入password:");
    // String sql = "select * from t_user where f_username = '"
    // + inputName + "' and f_password = '" + inputPwd + "'";
    // System.out.println(sql);
    // try {
    // Statement stat = con.createStatement();
    // ResultSet rs = stat.executeQuery(sql);
    // while(rs.next()){
    // theUser = new UserInfo();
    // theUser.setId(rs.getInt(1));
    // theUser.setUsername(rs.getString(2));
    // theUser.setPassword(rs.getString(3));
    // theUser.setAccount(rs.getDouble(4));
    // }
    //
    // } catch (SQLException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } finally{
    // if(con != null){
    // try {
    // con.close();
    // } catch (SQLException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
    // }
    //  //做一个推断来看成功是否
    // if(theUser == null){
    // JOptionPane.showMessageDialog(null, "登录失败!");
    // }else{
    // JOptionPane.showMessageDialog(null, "登录成功!

    您的剩余金额是:" + theUser.getAccount() + "元!

    ");
    // }

    //查询多条记录
    // ArrayList<UserInfo> allUsers = new ArrayList<UserInfo>();            //用一个集合来装
    // String sql = "select * from t_user where f_account > 12000";
    // try {
    // Statement stat = con.createStatement();
    // ResultSet rs = stat.executeQuery(sql);
    // while(rs.next()){
    // UserInfo theUser = new UserInfo();
    // theUser.setId(rs.getInt(1));
    // theUser.setUsername(rs.getString(2));
    // theUser.setPassword(rs.getString(3));
    // theUser.setAccount(rs.getDouble(4));
    //
    // allUsers.add(theUser);
    // }
    // } catch (SQLException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } finally{
    // if(con != null){
    // try {
    // con.close();
    // } catch (SQLException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
    // }


    //预编译语句
    // UserInfo theUser = null;
    // String inputName = JOptionPane.showInputDialog("请输入username:");
    // String inputPwd = JOptionPane.showInputDialog("请输入password:");
    // String sql = "select * from t_user where f_username = ?

    and f_password = ?

    ";
    // System.out.println(sql);
    // try {
    // PreparedStatement ps = con.prepareStatement(sql);
    // ps.setString(1, inputName);
    // ps.setString(2, inputPwd);
    //
    // ResultSet rs = ps.executeQuery();
    // while(rs.next()){
    // theUser = new UserInfo();
    // theUser.setId(rs.getInt(1));
    // theUser.setUsername(rs.getString(2));
    // theUser.setPassword(rs.getString(3));
    // theUser.setAccount(rs.getDouble(4));
    // }
    //
    // } catch (SQLException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } finally{
    // if(con != null){
    // try {
    // con.close();
    // } catch (SQLException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
    // }
    //
    // if(theUser == null){
    // JOptionPane.showMessageDialog(null, "登录失败!");
    // }else{
    // JOptionPane.showMessageDialog(null, "登录成功!您的剩余金额是:" + theUser.getAccount() + "元!");
    // }


    }


    }

  • 相关阅读:
    js全局变量
    $.getJSON异步请求和同步请求
    让js中的函数只有一次有效调用
    两个div并排显示,当浏览器界面缩小时会出现换行
    jquery获取窗口和文档的高度和宽度
    后台传带引号(")的数据需要注意
    C# dynamic
    (转)数据库函数解析JSON字符串
    Unicode和UTF-8
    用户通过浏览器修改表单隐藏域
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6940830.html
Copyright © 2011-2022 走看看