Mysql连接数据库
Posted 小魁的C世界
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql连接数据库相关的知识,希望对你有一定的参考价值。
1.获取数据库连接
package com.shop.Dbcon;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnction {
static final String DRIVERNAME="com.mysql.jdbc.Driver";
static final String URL="jdbc:mysql://localhost:3306/shopingdb?useSSL=false";
static final String NAME="root";
static final String PASSWORD="020823";
//加载驱动
static {
try {
Class.forName(DRIVERNAME);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getCon(){
Connection conn=null;
try {
conn=DriverManager.getConnection(URL, NAME, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(ResultSet rs,PreparedStatement ps,Connection conn) {
try {
if (rs!=null) {
rs.close();
}
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
DBConnction conn=new DBConnction();
System.out.println(conn);
}
}
2.增删改查操作
package com.win;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Connection;
import com.sql.DbConnection;
public class BASEDAO {
static Connection conn=null;
static java.sql.PreparedStatement ps=null;
static ResultSet rs=null;
public static String[][] getTableData(){
List<Emp> emplist=new ArrayList<Emp>();
try {
String sql="select * from tb_book";
conn=DbConnection.getcon();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while (rs.next()) {
Emp emp=new Emp();
emp.setId(rs.getString("id"));
emp.setBookName(rs.getString("bookName"));
emp.setAuthor(rs.getString("author"));
emp.setSex(rs.getString("sex"));
emp.setPrice(rs.getString("price"));
emp.setBookDesc(rs.getString("bookDesc"));
emplist.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}
String[][] dataStrings=new String[emplist.size()][6];
for (int i = 0; i < dataStrings.length; i++) {
dataStrings[i]=emplist.get(i).toArray();
}
return dataStrings;
}
public static void main(String[] args) {
BASEDAO dao=new BASEDAO();
String[][] result=dao.getTableData();
System.out.println(result);
}
public static boolean onAdd(Emp emp){
try {
String sql="insert into tb_book value(?,?,?,?,?,?)";
conn=DbConnection.getcon();
ps=conn.prepareStatement(sql);
ps.setString(1, emp.getId());
ps.setString(2, emp.getBookName());
ps.setString(3, emp.getAuthor());
ps.setString(4, emp.getSex());
ps.setString(5, emp.getPrice());
ps.setString(6, emp.getBookDesc());
return ps.executeUpdate()>0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public boolean deleteData(Emp emp) {
try {
String sql="delete from tb_book where id=?";
conn=DbConnection.getcon();
ps=conn.prepareStatement(sql);
ps.setString(1, emp.getId());
return ps.executeUpdate()>0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public String[][] findData(Object bookName) {
List<Emp> emplist=new ArrayList<Emp>();
try {
String sql="select * from tb_book where bookName like '%"+bookName+"%'";
conn=DbConnection.getcon();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while (rs.next()) {
Emp emp=new Emp();
emp.setId(rs.getString("id"));
emp.setBookName(rs.getString("bookName"));
emp.setAuthor(rs.getString("author"));
emp.setSex(rs.getString("sex"));
emp.setPrice(rs.getString("price"));
emp.setBookDesc(rs.getString("bookDesc"));
emplist.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}
String[][] dataStrings=new String[emplist.size()][6];
for (int i = 0; i < dataStrings.length; i++) {
dataStrings[i]=emplist.get(i).toArray();
}
return dataStrings;
}
}
3.获取数据库内容
package com.book.bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class TitleDao2 {
static Connection conn = null;// 连接接口
static PreparedStatement ps = null;// 执行语句接口
static ResultSet rs = null;// 结果集接口
// 查询结果集方法
public static String[][] getTableData() {
List<TitleEntity> empList = new ArrayList<TitleEntity>();
try {
String sql = "select * from tb_book";
conn = DbConnction.getConnction();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
TitleEntity tey = new TitleEntity();
tey.setIsbn(rs.getString("isbn"));
tey.setTitle(rs.getString("title"));
tey.setEditionNumber(rs.getString("editionNumber"));
tey.setCopyright(rs.getString("copyright"));
tey.setPublisherId(rs.getString("publisherID"));
tey.setCopyTime(rs.getString("imageFile"));
tey.setPrice(rs.getString("price"));
empList.add(tey);
}
} catch (SQLException e) {
e.printStackTrace();
}
String[][] dataStrings = new String[empList.size()][4];
for (int i = 0; i < dataStrings.length; i++) {
dataStrings[i] = empList.get(i).toArray();
}
return dataStrings;
}
}
<%@page import="com.book.bean.TitleDao2"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="dbcon" class="com.book.bean.DbConnction" scope="request" />
<jsp:useBean id="es" class="com.book.bean.TitleDao2" scope="request" />
<html>
<head>
<title>获取书本页面</title>
</head>
<body>
<table border="1">
<tr bgcolor="gray">
<td>ISBN</td>
<td>title</td>
<td>copyright</td>
<td>copyTime</td>
<td>editionNumber</td>
<td>publisherId</td>
<td>price</td>
</tr>
<%
Connection dbCon = dbcon.getConnction();
Statement stmt = dbCon.createStatement();
ResultSet rs = stmt.executeQuery("select * from tb_book");
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("isbn")%></td>
<td><%=rs.getString("title")%></td>
<td><%=rs.getString("copyright")%></td>
<td><%=rs.getString("editionNumber")%></td>
<td><%=rs.getString("copyTime")%></td>
<td><%=rs.getString("publisherId")%></td>
<td><%=rs.getString("price")%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
转换方法:
public String[] toArray() {
String[] dataStrings = new String[7];
dataStrings[0] = isbn;
dataStrings[1] = title;
dataStrings[2] = copyright;
dataStrings[3] = copyTime;
dataStrings[4] = String.valueOf(editionNumber);
dataStrings[5] = String.valueOf(publisherId);
dataStrings[6] = String.valueOf(price);
return dataStrings;
}
以上是关于Mysql连接数据库的主要内容,如果未能解决你的问题,请参考以下文章
错误:E/RecyclerView:未连接适配器;跳过片段上的布局
使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段