Java WEB之JDBC连接数据库的增删改查
2016-05-06 14:47
661 查看
基于Myeclipse的数据库操作(非tomcat里导入驱动程序)
1>新建java Web项目 右键点击项目工程new->folder 起名lib(用来存放jdbc驱动程序)
![](http://img.blog.csdn.net/20160506150235343?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2>找到驱动程序(mysql-connector-java-5.0.4-bin.jar)导入到lib中,右键点击驱动程序->build path->add build path,看到像奶瓶的文件就代表导入成功,即数据库驱动到此就可以用了
![](http://img.blog.csdn.net/20160506150308688?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20160506150342376?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3>打开mysql
create database books;
create table book(name varchar(15),id int);
![](http://img.blog.csdn.net/20160506150458159?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20160506150509143?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
4>写程序进行增删改查(以java程序为例,java web里面写大同小异)
package com;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Insert1(123456,"pan");
//Query1();
}
public static void Insert1(int id,String na) throws ClassNotFoundException, SQLException{
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
String sql="insert into b(name,id)values(?,?)";
PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
pst.setString(1,na);
pst.setInt(2,id);
pst.executeUpdate();
if(conn!=null){
conn.close();
}
}
public static void Query1() throws ClassNotFoundException, SQLException{
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
String sql="select * from b";
PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
ResultSet rs=(ResultSet) pst.executeQuery();
while(rs.next()){
int c=rs.getInt(2);
String name=rs.getString(1);
System.out.println(c+","+name+",");
}
if(conn!=null){
conn.close();
}
}
}
5>增加和查找编写成功了,以此为例
1>新建java Web项目 右键点击项目工程new->folder 起名lib(用来存放jdbc驱动程序)
2>找到驱动程序(mysql-connector-java-5.0.4-bin.jar)导入到lib中,右键点击驱动程序->build path->add build path,看到像奶瓶的文件就代表导入成功,即数据库驱动到此就可以用了
3>打开mysql
create database books;
create table book(name varchar(15),id int);
4>写程序进行增删改查(以java程序为例,java web里面写大同小异)
package com;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
public class test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Insert1(123456,"pan");
//Query1();
}
public static void Insert1(int id,String na) throws ClassNotFoundException, SQLException{
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
String sql="insert into b(name,id)values(?,?)";
PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
pst.setString(1,na);
pst.setInt(2,id);
pst.executeUpdate();
if(conn!=null){
conn.close();
}
}
public static void Query1() throws ClassNotFoundException, SQLException{
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","123456");
String sql="select * from b";
PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
ResultSet rs=(ResultSet) pst.executeQuery();
while(rs.next()){
int c=rs.getInt(2);
String name=rs.getString(1);
System.out.println(c+","+name+",");
}
if(conn!=null){
conn.close();
}
}
}
5>增加和查找编写成功了,以此为例
相关文章推荐
- Java性能调优笔记
- SpringMvc初步学习
- Java性能调优笔记
- .Net调用Java端带有WS-Security支持的Web Service各方案实战【转】
- 使用Eclipse Memory Analyzer进行内存泄漏分析三部曲
- java集合04--fail fast机制原理以及解决办法
- Java Collection知识总结
- 学习记录 Java常见的几种字符集以及对 AscII的了解
- JVM系列三:JVM参数设置、分析
- Java中获取文件大小的正确方法
- 第9次Java作业+LSYang
- Java 输入输出流
- JAVA学习之路--Tomacat安装及常见问题
- 69个经典Spring面试题和答案
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- java线程通讯——使用Lock和Condition代替synchronized 和 wait, notify notifyAll()
- Spring MVC @ResponseBody注解返回响应流时中文乱码问题的解决
- Eclipse的代码追踪功能