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() + "元!");
// }
}
}
mysql_jdbc的更多相关文章
- mysql_jdbc连接说明
mysql JDBC Driver 常用的有两个,一个是gjt(Giant Java Tree)组织提供的mysql驱动,其JDBC Driver名称(JAVA类名)为:org.gjt.mm.mysq ...
- spring web.xml 难点配置总结
web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...
- java连接mysql :No Suitable Driver Found For Jdbc 解决方法
今天出现编码出现了No suitable driver found for jdbc,又是找遍了网上的资料,基本上都说是以下个问题: 一是:连接URL格式出现了问题(Connection con ...
- No Suitable Driver Found For Jdbc_我的解决方法
转载自:http://www.blogjava.net/w2gavin/articles/217864.html 今天出现编码出现了No suitable driver found for ...
- no suitable driver found for jdbc:mysql//localhost:3306/..
出现这样的情况,一般有四种原因(网上查的): 一:连接URL格式出现了问题(Connection conn=DriverManager.getConnection("jdbc:mysql ...
- mybatis-配置文件mybatis-config.xml
在mybatis-config.xml中有初始的配置: <!-- 对事务的管理和连接池的配置 --> <environments default="development& ...
- MyBatis(一):配置并使用
MyBatis具体是什么东东,这些在后边在研究吧,本文目的是为了记录如何使用MyBatis. 首先,需要下载MyBatis开发所需要文件. 通过github上可以找到MyBatis代码:https:/ ...
- servlet 会话管理
一.URL 重写 URL 重写是一种会话跟踪技术,它将一个或多个token添加到URL的查询字符串中,每个token通常为 key=value形式,如下: url?key-1=value-1& ...
- spring web.xml 难点配置总结【转】
web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...
随机推荐
- DDos游戏行业受攻击最多
游戏行业遭遇DDOS攻击现状. 游戏一直是最易遭受黑客攻击的行业,高居全年DDOS攻击的48%.大规模攻击居多,平均值均超过100Gbps,攻击峰值急速上升,同比2015年增加了137.1%,其中攻击 ...
- Windows下使用python绘制caffe中.prototxt网络结构数据可视化
准备工具: 1. 已编译好的pycaffe 2. Anaconda(python2.7) 3. graphviz 4. pydot 1. graphviz安装 graphviz是贝尔实验室开发的一个 ...
- CAP定理在分布式系统设计中的最新应用
本文翻译自国外InfoQ和计算机杂志上一篇2012年旧文,本文就有关数据同步进行了讨论,特别关注业务事务的不变性与一致性如何在分布式系统中巧妙保证,探讨了长时间运行的事务的补偿机制.这些对分布式系统设 ...
- MVC获取当前Controller/Action名称
1.视图中获取: var actionName=ViewContext.RouteData.Values["action"].ToString().ToLower(); var c ...
- HD-ACM算法专攻系列(3)——Least Common Multiple
题目描述: 源码: /**/ #include"iostream" using namespace std; int MinComMultiple(int n, int m) { ...
- JavaScript设计模式(biaoyansu)
1.构造器模式——创建类模式 ES6:class Student{ constructor(score,quality){ this.score = score this.quality = qu ...
- HTML图片映射
<img>图片映射 <map>与<area>一起使用来定义一个图像映射(一个可点击的链接区域). <img src="cat.jpg" a ...
- bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易
题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯 ...
- CF209C Trails and Glades(欧拉路)
题意 最少添加多少条边,使无向图有欧拉回路. n,m≤106 题解 求出每个点的度数 奇度数点需要连一条新边 仅有偶度数点的连通块需要连两条新边 答案为上面统计的新边数 / 2 注意:此题默认以1为起 ...
- 关于git及其github的使用
一:序言(就是瞎扯) 人们都说不会使用git和github的程序员都不是好程序员,是的,当我第一次听到的时候有点失望.因为我也不会...但是这句话激起了我学习使用git的动力(其实也没怎么深入的学习) ...