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() + "元!");
// }
}
}
最新文章
- MongoDB学习笔记~根据子集合里某个属性排序
- jquery获取关于宽度和高度的尺寸问题
- USB
- noi题库(noi.openjudge.cn) 1.7编程基础之字符串T31——T35
- 【Origin】晨起忆梦
- Java学习笔记——static关键字与静态的使用方法
- Android给listview的item设定高度
- Hibernate Criterion
- (转)Ubuntu中使用dpkg安装deb文件提示依赖关系问题,仍未被配置
- Python_day1 基础语法
- 12块钱搭建一个ss(包括一个免费服务器)
- Android 之旅开始了!先自我了解下Android与Linux之间的关系
- java集合框架之HashMap
- ArcGIS对SLD样式的支持
- C++ bitset 常用函数及运算符
- FileInputStream、FileReader、FileWriter和File
- [快速幂][NOIP2012]转圈游戏
- 一步步实现windows版ijkplayer系列文章之七——终结篇(附源码)
- 在ros功能包CMakeLists.txt中获取所在功能包的路径 便于添加第三方库的相对路径
- Tomcat学习总结(4)——基于Tomcat7、Java、WebSocket的服务器推送聊天室