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, ...
随机推荐
- bzoj1002: [FJOI2007]轮状病毒(基尔霍夫矩阵)
1002: [FJOI2007]轮状病毒 题目:传送门 题解: 决定开始板刷的第一题... 看到这题的时候想:这不就是求有多少种最小生成树的方式吗? 不会啊!!!%题解... 什么鬼?基尔霍夫矩阵?? ...
- thinkphp5项目--个人博客(一)
thinkphp5项目--个人博客(一) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...
- matplotlib 可视化 —— 定制画布风格 Customizing plots with style sheets(plt.style)
Customizing plots with style sheets - Matplotlib 1.5.1 documentation 1. 使用和显示其他画布风格 >> import ...
- 25.不改变原生数据的STL algorithm
通过仿函数for_each操作 vector<,,,, }; list<double> db{ 1.1,2.2,3.3,4.4,5.5 }; //循环算法,算法的泛型 print p ...
- 安卓开发--HttpDemo02
package com.cnn.httpdemo02; import android.app.Activity; import android.os.Bundle; import android.vi ...
- Kali linux 2016.2(Rolling)里Metasploit的口令猜测与嗅探
不多说,直接上干货! 对于发现的系统与文件管理类网络服务,比如Telnet.SSH.FTP等,可以进行弱口令的猜测,以及对明文传输口令的嗅探,从而尝试获取直接通过这些服务进入目标网络的通道. 对于SS ...
- java9新特性-7-语法改进:接口的私有方法
1.官方Feature 213: Milling Project Coin Support for private methods in interfaces was briefly in consi ...
- PostgreSQL Replication之第三章 理解即时恢复(3)
3.3 做基础备份 在上一节中,您已经看到,启用归档只需要几行命令,并提供了极大的灵活性.在本节,我们将看到如何创建一个所谓的基础备份,稍后这可以使用XLOG.一个基本备份是一个最初的数据的拷贝. [ ...
- node使用express命令报错找不到ejs的解决方法
首先确定已经全局安装过好几遍express和express-generator,但一使用express命令直接报找不到ejs模块,全局和本地安装ejs都没用,nodemon模块报同样错误,找不到deb ...
- mpstat---用于多CPU环境下,显示各个可用CPU的状态
mpstat命令指令主要用于多CPU环境下,它显示各个可用CPU的状态系你想.这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查看所有CPU的平均状况信息,而且能够查看特定CPU ...