package com.dasenlin.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*测试jdbc
*/
public class Test {
private static final String url="jdbc:mysql://192.168.0.180/test";
private static final String user="yaya";
private static final String password="123qqq...A";
private static final String driverName="org.mariadb.jdbc.Driver";
public static void main(String[] args) {
Connection conn=null;
Statement state=null;
ResultSet result=null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
state =conn.createStatement();
//insert(state);
//update(state);
delete(state);
result =state.executeQuery("select * from shop;");
while(result.next()){
int id = result.getInt(1);
String name = result.getString(2);
System.out.println("id:"+id+"\tname:"+name);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(result!=null)result.close();
if(state!=null)state.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
*添加
* @throws SQLException
*/
public static void insert(Statement state) throws SQLException{
String sql = "insert into shop (id ,name) values(4,'汽车');";
state.execute(sql);
}
/**
*修改
* @throws SQLException
*/
public static void update(Statement state) throws SQLException{
String sql = "update shop set name='airplan' where id=4;";
int result = state.executeUpdate(sql);
if(result>0){
System.out.println("更新成功");
}
}
/**
*删除
* @throws SQLException
*/
public static void delete(Statement state) throws SQLException{
String sql ="delete from shop where id=4;";
state.execute(sql);
}
}
package com.dasenlin.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
*测试jdbc
*/
public class Test {
private static final String url="jdbc:mysql://192.168.0.180/test";
private static final String user="yaya";
private static final String password="123qqq...A";
private static final String driverName="org.mariadb.jdbc.Driver";
public static void main(String[] args) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet result=null;
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
//insert(conn);
//update(conn);
delete(conn);
ps=conn.prepareStatement("select * from shop");
result =ps.executeQuery();
while(result.next()){
int id = result.getInt(1);
String name = result.getString(2);
System.out.println("id:"+id+"\tname:"+name);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(result!=null)result.close();
if(ps!=null)ps.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
*添加
* @throws SQLException
*/
public static void insert(Connection conn) throws SQLException{
String sql = "insert into shop (id ,name) values(?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, 5);
ps.setString(2, "汽车");
ps.executeUpdate();
}
/**
*修改
* @throws SQLException
*/
public static void update(Connection conn) throws SQLException{
String sql = "update shop set name=? where id=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, "sir");
ps.setInt(2, 4);
int result = ps.executeUpdate();
if(result>0){
System.out.println("更新成功");
}
}
/**
*删除
* @throws SQLException
*/
public static void delete(Connection conn) throws SQLException{
String sql ="delete from shop where id=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, 4);
ps.executeUpdate();
}
}