目录标题
- 力扣练习题1. 两数之和
- main主函数的类代码如下
- Java连接SQL Server并实现数据库增删改查方法库
- SQL Server里数据库liuhao里的dbo.table1数据表如下表所示
力扣练习题1. 两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
力扣官方网址https://leetcode-cn.com/problems/two-sum/
main主函数的类代码如下
import java.util.Scanner;
public class Methodliu
{
public static void main(String[] args)
{
Solution.productInt(10, 30);
//Methodliu.productInt(55, 60);
//Methodliu.productInt(70, 75);
Methodliu.substractInt(3,2);
sumInt(50,40);
devideInt(60,30);
//int[] nums= new int[3];//{0,0,0}
int[] nums={2,7,11,15};
System.out.println("nums:"+nums[0] + "," +nums[1]);
System.out.println("length:"+nums.length);
Scanner sc = new Scanner(System.in);
int[]result= Solution.twoSum2(sc);
System.out.println("result:"+result[0]+","+result[1]);
/* while(sc.hasNext()){
String c = sc.next();
for(int i=0;i<c.length();i++){
System.out.println(c.charAt(i));
}
}*/
// int[] nums={2,7,11,15};
//int[] result= Solution.twoSum(nums,9);
// System.out.println("result:"+result[0]+","+result[1]);
}
/*
public static void productInt(int a,int b)
{
int c = a * b;
System.out.println(a + "*" + b + "=" + c);
}*/
public static void substractInt(int m,int n){
int q=m-n;
System.out.println(m + "-" + n + "=" + q);
}
public static void sumInt(int m,int n){
int q=m+n;
System.out.println(m + "+" + n + "=" + q);
}
public static void devideInt(int m,int n){
int q=m/n;
System.out.println(m + "/" + n + "=" + q);
}
}
Solution类,下面展示 代码片
.
import java.util.Scanner;
public class Solution
{
public static void productInt(int a,int b)
{
int c = a * b;
System.out.println(a + "*" + b + "=" + c);
}
public static void substractInt(int m,int n){
int q=m-n;
System.out.println(m + "-" + n + "=" + q);
}
public static void sumInt(int m,int n){
int q=m+n;
System.out.println(m + "+" + n + "=" + q);
}
public static void devideInt(int m,int n){
int q=m/n;
System.out.println(m + "/" + n + "=" + q);
}
public static int[]
twoSum(int[] nums, int target){
int[] rtn=new int[2];
for(int i=0;i< nums.length;i++){//nums.length=4
for(int j=i+1;j<nums.length;j++){
// if(i<j){
if((nums[i]+nums[j])==target){
rtn[0]=i;
rtn[1]=j;
}
// }
}
}
return rtn;
}
public static int[]
twoSum2(Scanner sc){
String c="";
String c2="";
int[] nums=new int[5];
if(sc.hasNext()){
c=sc.next();//{1123,2,2,3}
//str="{2,7,11,15,20}" target=26
// str.substring(1,str.indexOf(","))
}
if(sc.hasNext()){
c2=sc.next();
}
if(c.indexOf(",")!=-1) {
String str = c.substring(1, c.indexOf(","));
System.out.println(c.indexOf(","));
int num0 = Integer.valueOf(str);//num0=1123
nums[0]=num0;
int i=1;
c = c.substring(c.indexOf(",") + 1);
while (c.indexOf(',')!=-1){
if(i<5){
str = c.substring(0, c.indexOf(","));
num0 = Integer.valueOf(str);//num0=1123
nums[i]=num0;
c = c.substring(c.indexOf(",") + 1);
}
i++;
}
nums[i]=Integer.valueOf(c.substring(0,1));
}else{
return null;
}
int target=Integer.valueOf(c2);
int[] rtn=new int[2];
for(int i=0;i< nums.length;i++){//nums.length=4
for(int j=i+1;j<nums.length;j++){
// if(i<j){
if((nums[i]+nums[j])==target){
rtn[0]=i;
rtn[1]=j;
}
// }
}
}
return rtn;
}
}
Java连接SQL Server并实现数据库增删改查方法库
- src
- Deu_tils
- Dologin
- student
- Text
Deu_tils.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Deu_tils {
/**
* 得到资源的方法
* @return 资源
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=liuhao";
String userName="sa";
String userPwd="abcd123";
Connection dbConn=null;
try
{
Class.forName(driverName);
dbConn= DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("success!");
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("fail!");
}
return dbConn;
}
}
Dologin.java
import java.sql.Connection;
import java.sql.*;
public class Dologin {
private String sql;
private PreparedStatement pstmt;
private Connection conn;
/**
* 增加数据库里面一条学生对象信息的方法
*
* @param s
* @return
*/
public void Insert(student s) throws SQLException {
Connection conn = Deu_tils.getConnection();
//int i = 0;
sql = "INSERT INTO liuhao.dbo.table1 (id , name, sex, brithday) VALUES (?,?,?,?)";
System.out.println(s.getId());
pstmt =conn.prepareStatement(sql);
pstmt.setInt(1, s.getId());
pstmt.setString(2, s.getName());
pstmt.setString(3, s.getSex());
pstmt.setString(4, s.getBrithday());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
/**
*
* 根据数据库id改名字性别和生日的方法
* @param s
* @return
* @throws SQLException
*/
public void Update(int id,student s) throws SQLException {
Connection conn = Deu_tils.getConnection();
int i = 0;
sql ="UPDATE liuhao.dbo.table1 SET name ='"+s.getName()+"',sex= '"+s.getSex()+"',brithday= '"+s.getBrithday()+"' WHERE id = "+id;
pstmt =conn.prepareStatement(sql);
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
}
/**
* 根据对象名删除数据库对象信息的方法
* @param name
* @return
* @throws SQLException
*/
public void Delete(String name) throws SQLException {
Connection conn = Deu_tils.getConnection();
int i = 0;
sql ="DELETE FROM liuhao.dbo.table1 WHERE name = '"+name+"'";
pstmt =conn.prepareStatement(sql);
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
}
/**
* 查询表格的方法
* @throws SQLException
*/
public void Select() throws SQLException {
Connection conn = Deu_tils.getConnection();
sql = "SELECT * FROM dbo.table1;";
pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
student s = new student();
while (rs.next()) {
System.out.println(rs.getObject("id"));
System.out.println(rs.getObject("name"));
System.out.println(rs.getObject("sex"));
System.out.println(rs.getObject("brithday"));
System.out.println("---------------------------");
}
conn.close();
pstmt.close();
rs.close();
}
}
student.java
public class student {
private int id;
private String name;
private String sex;
private String brithday;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public student() {
}
public student(int id,String name, String sex, String brithday) {
this.id=id;
this.name = name;
this.sex = sex;
this.brithday = brithday;
}
public String getBrithday() {
return brithday;
}
public void setBrithday(String brithday) {
this.brithday = brithday;
}
@Override
public String toString() {
return "student{" +"id='" + id+ '\'' +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", brithday='" + brithday + '\'' +
'}';
}
}
Text.java
import java.sql.*;
import java.util.Scanner;
public class Text {
public static void main(String[] args) throws SQLException {
Scanner sc=new Scanner(System.in);
System.out.println("请输入录入对象的id:");
int ids=sc.nextInt();
System.out.println("请输入录入对象的名字:");
String names=sc.next();
System.out.println("请输入录入对象的性别:");
String sexs=sc.next();
System.out.println("请输入录入对象的生日:");
String bris=sc.next();
student s1=new student(ids,names,sexs,bris);
Dologin dl1=new Dologin();
dl1.Insert(s1);
dl1.Select();
System.out.println("请输入要改对象的id:");
int id=sc.nextInt();
System.out.println("请输入要改的数据值名字:");
String name=sc.next();
System.out.println("请输入要改的数据值性别:");
String sex=sc.next();
System.out.println("请输入要改的数据值生日:");
String bri=sc.next();
student s2=new student(id,name,sex,bri);
Dologin dl2=new Dologin();
dl2.Update(id,s2);
dl2.Select();
System.out.println("请输入您要删除的对象姓名:");
String namess=sc.next();
Dologin dl3=new Dologin();
dl3.Delete(namess);
dl3.Select();
}
}
SQL Server里数据库liuhao里的dbo.table1数据表如下表所示
id | name | sex | birthday |
NULL | 张三 | 男 | 1999-12-15 |
NULL | 李四 | 男 | 1999-08-16 |
NULL | 王五 | 女 | 1999-022-16 |
152535 | 利达 | 男 | 1659-14-556 |
225566 | 王八 | 男 | 1999-12-15 |
123456 | 来了 | 男 | 1999-12-14 |
123654 | nansak男生 | 女 | 1999-2-56 |