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, ...
随机推荐
- 安卓开发--sharedpreferences存储数据
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s ...
- BZOJ 1927 最小费用流问题
From lydrainbowcat //By SiriusRen #include <queue> #include <cstdio> #include <cstrin ...
- BZOJ 2127: happiness(最小割解决集合划分)
Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 2350 Solved: 1138[Submit][Status][Discuss] Descript ...
- Android框架-Volley(三)
经过前面两篇文章的学习,我们已经掌握了Volley各种Request的使用方法,包括StringRequest.JsonRequest.ImageRequest等.其中StringRequest用于请 ...
- js字符串排序方法
前端开发过程中有时需自己手写排序方法 一般想到数字的字符串排序方法 我们会用到 var newArr = arr. sort(function(a,b){return a - b})来进行排序 但除此 ...
- 【Uva 11400】Lighting System Design
[Link]: [Description] 你要构建一个供电系统; 给你n种灯泡来构建这么一个系统; 每种灯泡有4个参数 1.灯泡的工作电压 2.灯泡的所需的电源的花费(只要买一个电源就能供这种灯泡的 ...
- ArcGIS api for javascript——鼠标悬停时显示信息窗口
描述 本例展示当用户在要素上悬停鼠标时如何显示InfoWindow.本例中,要素是查询USA州图层的QueryTask的查询结果.工作流程如下: 1.用户单击一个要素 2.要素是“加亮的”图形. 3. ...
- Java Exception和Error的差别
Java中异常的抽象类是Throwable,在此基础上.派生出两大类:Error和Exception. Error是程序中的严重错误,不应该用try-catch包括.Javadoc的说明例如以下: A ...
- JSunpack-n模拟WireShark拦截文件传输
前言: 在前面的实验里我们进行了JSunpack-n的安装及其简单使用.JSunpack-n还有另外一些功能须要进行測试试验,由于本人也是刚接触这些东西.本文就当中一个"功能点"进 ...
- Spring MVC数据转换
样例:把一个字符串封装而一个对象. 如:username:password格式的数据ZhangSan:1234.我们把这个数据封装成一个User对象.以下分别使用属性编辑器与转换器来实现. 1.自己定 ...