您的位置:首页 > 数据库 > MySQL

mysql_jdbc

2015-08-18 23:38 591 查看
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/数据库名",         
"账号", "密码");

} 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("请输入用户名:");

// String inputPwd = JOptionPane.showInputDialog("请输入密码:");

// 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("请输入用户名:");

// String inputPwd = JOptionPane.showInputDialog("请输入密码:");

// String sql = "select * from t_user where f_username = ? and f_password = ?";

// <
4000
/span>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() + "元!");

// }

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java