验证中...
语言: Java
分类: 数据库相关
最后更新于 2018-04-17 00:48
Jdbctest.java
原始数据 复制代码
package com.lmj.week06;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import javax.naming.spi.DirStateFactory.Result;
import org.junit.Test;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class Jdbctest {
@Test
public void selectUsers() {
String driverClassName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/mysql";
String username="root";
String password="19980716";
java.sql.Connection conn=null ;
String sql=null;
java.sql.PreparedStatement pstms=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url,username,password);
sql="select * from userinfor";
pstms=conn.prepareStatement(sql);
rs=pstms.executeQuery();
while(rs.next()) {
String shopname=rs.getString("shopname");
int id=rs.getInt("id");
String kind=rs.getString("kind");
System.out.println(id+"-"+shopname+"-"+kind);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstms!=null) {
try {
pstms.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void insertUsers() {
String driverClassName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/mysql";
String username="root";
String password="19980716";
java.sql.Connection conn=null ;
String sql=null;
String sqlcheck=null;
java.sql.PreparedStatement pstms=null;
java.sql.PreparedStatement pstms1=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url,username,password);
sql="insert into userinfor(shopname,kind)values(?,?)";
pstms=conn.prepareStatement(sql);
System.out.println("请输入要插入的商品信息:(例:手表-配饰)");
Scanner in=new Scanner(System.in);
String[] instring=in.nextLine().split("-");
sqlcheck="select kind from userinfor";
pstms1=conn.prepareStatement(sqlcheck);
rs=pstms1.executeQuery();
int flag=1;
while(rs.next()) {
String kind=rs.getString("kind");
if(kind.equals(instring[1])) {
flag=0;
break;
}
}
if(flag==1) {
pstms.setString(1, instring[0]);
pstms.setString(2, instring[1]);
int result=pstms.executeUpdate();
if(result > 0) {
System.out.println("add ok");
}
else {
System.out.println("add no ok");
}
}
else {
System.out.println("kind have exist!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstms!=null) {
try {
pstms.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助